+ 2
Why is my class method not printing anything in Python?
I'm trying to define a class Book with a method display_info(). Here's my simplified code: class Book: def __init__(self, title): self.title = title def display_info(title): print(title) book1 = Book("1984") book1.display_info() But when I run it, nothing prints. What am I missing?
3 Answers
+ 6
you need to use 'self'
class Book:
def __init__(self, title):
self.title = title
def display_info(self):
print(self.title)
book1 = Book("1984")
book1.display_info()
+ 1
Use `self` in the first parameter.