Python Practice › OOP Programs › Program 44

Book class

EasyOOP Programs · 44 of 48

Create Book with title, author and pages, with getter methods.

Advertisement
Output
Wings of Fire | A. P. J. Abdul Kalam | 180

Approach

  1. One method per attribute.

Solution

Python
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.

More oop programs

← All 48 OOP Programs

Leave a Comment