Lecture notes · Programming in Python · Lecture 19

Inheritance

MIT 6.100L Intro to CS and Programming using Python, lecture 19, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part recaps how a Python class groups objects that share properties and behaviors, then shows how an Animal class is built with an init method, data storage, a printing method, and getters and setters.

Key ideas

Words to know

class
A blueprint you write to define a new kind of object, such as an Animal, that says what information and actions each object of that kind has.
init method
A special setup method that runs when a new object is created. It sets up the object's starting information.
instance variable
A value stored on one specific object, written with self. It is kept for as long as that object exists.
getter
A short method that gives you back the current value of one of an object's stored values, without changing it.

Example

Think of a blank form for a cat with spaces for name, age, and color. Every cat you fill in uses the same form, so you know what to expect from each one. A wild rabbit uses a similar form, but the name space is left empty because rabbits here are not given names.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains why you should change and read an object's information only through its getter and setter methods, not by reaching directly into its data attributes.

Key ideas

Words to know

data attribute
A piece of information stored inside an object, such as an animal's age or name.
getter
A method whose job is to hand back the value of a data attribute, such as get_age.
setter
A method whose job is to change the value of a data attribute, such as set_name.
default parameter
A value a function uses automatically when you do not pass one in. Here, the name defaults to an empty string.

Example

Think of a TV remote. The buttons let you change the channel or volume without you knowing how the TV works inside. If the TV maker changes the wiring, the remote can still work, but a person who pokes at the wires directly may break the TV.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to build a dictionary of animal objects and a list of animal objects, and how to check that the results are correct.

Key ideas

Words to know

dictionary
A collection that stores values under keys. You look up a value by its key, like finding a word in a dictionary by its spelling.
index
The position number of an item in a list, starting from 0 for the first item.
setter
A function that changes the value of one piece of an object, such as its name. It is the approved way to change that value, instead of changing it directly.
dunder str
A special method, written as __str__, that tells Python how to turn an object into readable text when it is printed.

Example

Think of a coat check at a theater. Each coat gets a ticket number, which is like a dictionary key, and the coat is the value. If you look at the stack of tickets, you learn the numbers but not what is in each coat. To see the coats themselves, you have to go through them one at a time.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains inheritance, where a more specific class such as cat or student automatically gets the data and behaviors of a more general class such as animal.

Key ideas

Words to know

subclass
A more specific class that is built from a more general class. It is also called a child class.
parent class
The general class that other classes are built from. It is also called the base class or superclass.
inherit
To automatically get the data and behaviors of a parent class without writing them again.
override
To replace an inherited behavior with a new version in the child class.

Example

Think of a general category called vehicle, with things every vehicle has, such as wheels and an engine. A truck is a kind of vehicle, so it gets wheels and an engine automatically. A truck can also add its own feature, like a cargo bed.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part shows how a child class (like cat or person) gets everything from its parent class (animal), and how it can replace or add to those parts.

Key ideas

Words to know

inherit
To get something from a parent. A child class gets the methods and data of its parent class without writing them again.
override
To replace a parent's method with a new version in the child class that has the same name.
parent class
The more general class that another class is built from. For example, animal is the parent of cat.
data attribute
A piece of information stored inside an object, such as the age or name of an animal.

Example

Think of a family where a child inherits the family's last name and eye color without having to earn them. If the child decides to use a different nickname, that is like overriding: the child uses its own version instead of the parent's. If the family never had a certain trait, the child does not have it either.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

Inheritance lets a child class reuse what its parent class has, change some of it, and add new things of its own.

Key ideas

Words to know

subclass
A class built on top of another class. It is also called a child class.
inherit
To get the attributes and methods of a parent class without writing them again.
override
To replace a parent's method with a new version in the child class.
class variable
A variable shared by all objects made from a class, like one common resource everyone can use.

Example

Think of a family where a child gets a parent's last name, eye color, and habits automatically. The child can still change some things, like how they greet people, and can learn new skills the parent never had. In the lesson, a Student gets everything a Person has, changes how it speaks, and adds a major.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows how a class variable can act as a shared counter, so each new rabbit gets its own ID number, and how adding two rabbits with plus creates a new rabbit that has both as parents.

Key ideas

Words to know

class variable
A variable that belongs to the whole class, not to one object. All objects of that class share it, so a change made by one shows up for all of them.
instance
One object made from a class. Each rabbit you create is a separate instance of the rabbit class.
init method
The special method that runs when you create a new object. It sets up the object's starting data, such as age and parents.
dunder method
A special method whose name starts and ends with double underscores, such as the add method. Python calls it automatically when you use certain symbols, like plus, on your objects.

Example

Think of a numbered ticket machine at a deli counter. Every customer who takes a ticket gets the number the machine is showing, and the machine then moves up by one. The machine is shared, so the next customer always gets the next number, and no two customers get the same one.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:16:41

This part shows how adding two rabbits with + creates a new rabbit, and how the program decides when two rabbits count as equal.

Key ideas

Words to know

dunder method
A special method with double underscores on each side of its name. Python runs it automatically when you use certain symbols, like + or ==, on your objects.
init method
The setup method that runs when a new object is created. It sets the starting values, such as the age and parents of a new rabbit.
Boolean
A simple yes-or-no value. It is either True or False.
class variable
A variable that belongs to the whole class and is shared by every object made from it, instead of being separate for each object.

Example

Imagine a school where every new student gets the next number from one shared clipboard, whether they enroll at the front desk or transfer in from another school. Each new student takes the next number, and the clipboard moves forward by one. This is only an analogy to help the idea of a shared counter click.

3 checked practice questions for this part are in the app.

← Lecture 18: More Python Class Methods · Lecture 21: Timing Programs and Counting Operations →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York