Objective
By the end of this activity, you will:
- Understand how to define a class in Python.
- Create a constructor method (
__init__) to initialize object attributes. - Define and use instance methods.
- Create objects (instances) of your class and call methods on them.
Activity Instructions
Step 1: Create a Basic Class
- Open your Python environment (codeboard.io).
- Start by defining a class called
Book.
class Book:
pass # We'll fill this in as we go!
Step 2: Add Attributes Using the __init__ Method
- In the
Bookclass, add an__init__method. This method will initialize the attributes of theBookclass. - Each book should have the following attributes:
title: The title of the book.author: The author of the book.pages: The number of pages in the book.read: A boolean attribute to track if the book has been read (default toFalse).
- Update your class:
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.read = False # Default value
Step 3: Create an Instance of the Book Class
- Now, let’s create a
Bookobject.
my_book = Book("The Great Gatsby", "F. Scott Fitzgerald", 180)
- Print the
title,author, andpagesattributes ofmy_bookto confirm they are set correctly:
print("Title:", my_book.title)
print("Author:", my_book.author)
print("Pages:", my_book.pages)
print("Read:", my_book.read)
Step 4: Add a Method to Mark the Book as Read
- Add a method called
mark_as_readto theBookclass. This method should change thereadattribute toTrue. - Update your class with the new method:
class Book:
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.read = False
def mark_as_read(self):
self.read = True
- Now, call the
mark_as_readmethod onmy_bookand check if it worked:
my_book.mark_as_read()
print("Read:", my_book.read)
Step 5: Add a __str__ Method
- The
__str__method is used to provide a readable string representation of an object. - Add a
__str__method to display the book’s information:
class Book:return "Title: "+self.title+", Author: "+self.author+", Pages: "+str(self.pages)+", Read: "
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.read = False
def mark_as_read(self):
self.read = True
def __str__(self):
read_status = "Yes" if self.read else "No"
- Now, print the
my_bookobject:
print(my_book)
Step 6: Add a Class and Instance Variables
- Create a class called
Book. - Add a class variable called
total_books, which will keep track of the total number of books created.
class Book:
# Class variable to keep track of the total number of books
total_books = 0
def __init__(self, title, author, pages):
self.title = title # Instance variable
self.author = author # Instance variable
self.pages = pages # Instance variable
self.read = False # Instance variable to track if the book has been read
Book.total_books += 1 # Update the class variable
# Test by creating a few book objects and printing the total_books
book1 = Book("The Catcher in the Rye", "J.D. Salinger", 234)
book2 = Book("To Kill a Mockingbird", "Harper Lee", 281)
print("Total books created:", Book.total_books)
Discussion Point: Notice that total_books is a class variable. This means it’s shared among all instances of Book. The instance variables (title, author, pages, read) are unique to each object.
Step 7: Understanding Local Variables vs. Instance Variables
- Add a method called
get_summaryto theBookclass that briefly describes the book. - Inside
get_summary, create a local variable calledsummary.
class Book:
total_books = 0
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.read = False
Book.total_books += 1
def get_summary(self):
summary = f"{self.title} by {self.author}, {self.pages} pages." # Local variable
return summary
# Test by creating a book and calling get_summary
book = Book("1984", "George Orwell", 328)
print(book.get_summary())
Discussion Point: Here, summary is a local variable—it exists only within the get_summary method. In contrast, title, author, and pages are instance variables that are available to other methods in the Book class.
Step 8: Add Getters, Setters, and Mutators
- Add getter methods for each attribute (e.g.,
get_title,get_author,get_pages). - Add setter methods for each attribute (e.g.,
set_title,set_pages). - Add a mutator method called
mark_as_readto change thereadstatus toTrue.
class Book:
total_books = 0
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
self.read = False
Book.total_books += 1
# Getter methods
def get_title(self):
return self.title
def get_author(self):
return self.author
def get_pages(self):
return self.pages
# Setter methods
def set_title(self, title):
self.title = title
def set_pages(self, pages):
self.pages = pages
# Mutator to mark the book as read
def mark_as_read(self):
self.read = True
# Test the getters, setters, and mutator
book = Book("Pride and Prejudice", "Jane Austen", 432)
print("Title:", book.get_title())
book.set_title("Pride &
Step 9: Test Your Knowledge
- Create more books: Create additional instances of
Bookwith different titles, authors, and page counts. - Mark books as read: Use the
mark_as_readmethod on some books and print them to see the change. - Add a new method: Add a method to the
Bookclass that calculates the estimated time to finish reading based on a given reading speed (e.g.,time_to_finish(reading_speed), wherereading_speedis pages per hour).
Step 10: Bonus Challenge
- Create a
LibraryClass:- This class should store multiple
Bookobjects in a list. - It should have methods to:
- Add a book to the library.
- Display all books in the library.
- List only books that haven’t been read.
- This class should store multiple
Example Solution for the Bonus Challenge
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def display_books(self):
for book in self.books:
print(book)
def display_unread_books(self):
print("Unread Books:")
for book in self.books:
if not book.read:
print(book)
# Example Usage
library = Library()
library.add_book(my_book)
library.display_books()
library.display_unread_books()
Reflection Questions
- What is the purpose of the
selfparameter in class methods? - Why do we use the
__init__method? - How does the
mark_as_readmethod change the object’s state?
