Home > Enterprise >  Function not defined - Python
Function not defined - Python

Time:09-22

I'm trying to create a class in Python and the pLink function is giving a "not defined" error message when being called.

class Book:
    def __init__(bk, name, ISBN, genre, form, rating, author, language, publisher, link):
        bk.name = name
        bk.ISBN = ISBN
        bk.genre = genre
        bk.form = form
        bk.rating = rating
        bk.author = author
        bk.language = language
        bk.publisher = publisher
        bk.link = link
        
    def __repr__(bk):
        rep = 'Book:      '   bk.name   "\n"   "ISBN:      "   str(bk.ISBN)   "\n"   "Genre:     "   bk.genre   "\n"   "Format:    "   bk.form   "\n"   "Rating:    "   str(bk.rating)   "\n"   "Author:    "   bk.author   "\n"   "Language:  "   bk.language   "\n"   "Publisher: "   bk.publisher   "\n" ''
        return rep
    
    def pLink(bk):
        pl = "Here's the link to purchase the book: "   bk.link   ""
        return pl
    
b1 = Book("Where the Red Fern Grows", 9780399551239, "Children's Literature", "Hardcover", 4.1, a1.name, "English", "Doubleday", "https://www.amazon.com/Where-Fern-Grows-Wilson-Rawls/dp/0399551239/ref=sr_1_1?crid=2UQWGKLQ7KB7F&keywords=where the red fern grows hardcover&qid=1663797713&sprefix=where the red fern grows hardcove,aps,107&sr=8-1")

enter image description here

Any ideas on what I'm missing here? It works if I don't indent the function.

CodePudding user response:

You need to call that function from the class.

You can not use any function inside a class from outside you need to go in the class and call it

And in this case you need to create an object and call your function through it

Obj = Book()
Obj.pLink()

CodePudding user response:

The closest way you have done that I can think of is this:

class Book:
    def __init__(bk, name, ISBN, genre, form, rating, author, language, publisher, link):
        bk.name = name
        bk.ISBN = ISBN
        bk.genre = genre
        bk.form = form
        bk.rating = rating
        bk.author = author
        bk.language = language
        bk.publisher = publisher
        bk.link = link
        
    def __repr__(bk):
        rep = 'Book:      '   bk.name   "\n"   "ISBN:      "   str(bk.ISBN)   "\n"   "Genre:     "   bk.genre   "\n"   "Format:    "   bk.form   "\n"   "Rating:    "   str(bk.rating)   "\n"   "Author:    "   bk.author   "\n"   "Language:  "   bk.language   "\n"   "Publisher: "   bk.publisher   "\n" ''
        return rep
    
    @staticmethod
    def pLink(bk):
        pl = "Here's the link to purchase the book: "   bk.link   ""
        return pl
    
b1 = Book("Where the Red Fern Grows", 9780399551239, "Children's Literature", "Hardcover", 4.1, a1.name, "English", "Doubleday", "https://www.amazon.com/Where-Fern-Grows-Wilson-Rawls/dp/0399551239/ref=sr_1_1?crid=2UQWGKLQ7KB7F&keywords=where the red fern grows hardcover&qid=1663797713&sprefix=where the red fern grows hardcove,aps,107&sr=8-1")

Now you can use:

print(Book.pLink(b1))
  • Related