Python Practice › OOP Programs › Program 44
Book class
Create Book with title, author and pages, with getter methods.
Output
Wings of Fire | A. P. J. Abdul Kalam | 180
Approach
- One method per attribute.
Solution
class Book: def __init__(self, title, author, pages): self.title, self.author, self.pages = title, author, pages def get_title(self): return self.title def get_author(self): return self.author def get_pages(self): return self.pages b = Book("Wings of Fire", "A. P. J. Abdul Kalam", 180)print(b.get_title(), "|", b.get_author(), "|", b.get_pages())Output
Wings of Fire | A. P. J. Abdul Kalam | 180
Tip
In idiomatic Python you would usually read b.title directly and add @property only when extra logic is needed.
Learn the concept
New to this topic? Read the lesson, then come back.