PCC·09 · Chapter record
Classes
Chapter 9 — modeling real things as objects
Five sessions on object-oriented Python: writing a class and making instances from it, default attributes and the three ways to modify them, inheritance, overriding a parent's method, and building classes out of other classes. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.
Creating and Using a Class
Concept
- A class is a set of instructions for making an object — by convention its name is capitalized, like
Dog. __init__()is a special method Python runs automatically every time you create a new instance — it sets up that instance's starting attributes.- The first parameter of every method is
self— a reference to the specific instance the method was called on. Python passes it automatically; you never supply it yourself. self.name = nameattaches a value to the instance as an attribute — from then on, every method (and you, from outside) can read it asinstance.name.- Each instance is independent — creating a second Dog doesn't affect the first one's attributes at all.
Key Terms
- class
- a blueprint for creating objects
- instance
- one specific object made from a class
- __init__()
- runs automatically when a new instance is created
- self
- a reference to the instance a method was called on
- attribute
- a value attached to an instance via self.name = value
Exercise
Make a class Restaurant with restaurant_name and cuisine_type attributes, plus a describe_restaurant() method. Create an instance and call the method.
Create three different Restaurant instances and call describe_restaurant() for each — confirm they stay independent.
Default Values & Modifying Attributes
Concept
- An attribute can get its value inside
__init__()without being passed in — likeself.odometer_reading = 0, a sensible starting default. - You can change an attribute's value directly through an instance:
my_car.odometer_reading = 23. - Or write a method that updates it for you — which lets you add validation, like refusing to roll the odometer back.
- Or write a method that increments it by some amount —
self.odometer_reading += miles— rather than replacing the value outright.
Key Terms
- default attribute
- an attribute set inside __init__() without a parameter
Exercise
Add a number_served attribute (default 0) to your Restaurant class, plus set_number_served() and increment_number_served() methods.
Try to set a car's odometer to a lower number than it currently reads, and confirm the rollback-protection message appears.
Inheritance
Concept
- A class can inherit from another:
class ElectricCar(Car):makes ElectricCar a child of the parent class Car. - Call
super().__init__(...)inside the child's__init__()to run the parent's setup — it initializes every attribute the parent defines, so you don't repeat that code. - The child inherits every method the parent has — a plain ElectricCar can already do everything a Car can, with zero extra code.
- Then add whatever's specific to the child — like
self.battery_size, which a plain Car doesn't have.
Key Terms
- parent class / child class
- the class being inherited from, and the class that inherits
- super().__init__()
- calls the parent class's __init__() from inside the child
Exercise
Write a class that inherits from one of your earlier classes (e.g. an IceCreamStand(Restaurant)) and adds one new attribute specific to the child.
Overriding Methods
Concept
- Define a method in the child class with the same name as one in the parent, and Python uses the child's version instead — this is overriding.
- It's the right move whenever the parent's behavior doesn't make sense for the child — an electric car filling a gas tank, for instance.
- The parent's version of the method is still there — it's just ignored when you call it on an instance of the child.
Key Terms
- overriding
- defining a method in the child class to replace the parent's version
Exercise
Give your parent class a method that doesn't make sense for one of your child classes, then override it there with more appropriate behavior.
Instances as Attributes
Concept
- When a class grows too many attributes and methods around one sub-topic, pull that sub-topic into its own class — like moving all the battery logic out of ElectricCar into a Battery class.
- Then store an instance of that class as an attribute:
self.battery = Battery(). - To reach it from outside, chain through the attribute:
my_tesla.battery.describe_battery()— instance, then its attribute, then a method on that.
Key Terms
- composition
- building a class out of instances of other classes
Exercise
Split one of your classes' more detailed attributes into a separate helper class (like an Engine, or a Menu), then store an instance of it as an attribute and reach it with dot-chaining.