Python Practice › OOP Programs › Program 45
EBook with open and close
Create EBook (inherits Book) with file_size and format, and methods to open and close the book.
Output
Opened Python Basics (PDF, 3.4 MB) Closed Python Basics
Approach
- Track whether the e-book is open.
Solution
class Book: def __init__(self, title, author, pages): self.title, self.author, self.pages = title, author, pages class EBook(Book): def __init__(self, title, author, pages, file_size_mb, fmt): super().__init__(title, author, pages) self.file_size_mb, self.format = file_size_mb, fmt self.is_open = False def open(self): self.is_open = True return f"Opened {self.title} ({self.format}, {self.file_size_mb} MB)" def close(self): self.is_open = False return f"Closed {self.title}" e = EBook("Python Basics", "SQA Tools", 220, 3.4, "PDF")print(e.open())print(e.close())Output
Opened Python Basics (PDF, 3.4 MB) Closed Python Basics
Learn the concept
New to this topic? Read the lesson, then come back.