+ 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?

2nd Jun 2025, 9:02 PM
Meerim Akmatova
Meerim Akmatova - avatar
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()
2nd Jun 2025, 9:10 PM
Bob_Li
Bob_Li - avatar
+ 1
Use `self` in the first parameter.
3rd Jun 2025, 12:24 PM
Ushasi Bhattacharya