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 freeWatch 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
- Real-world objects can be sorted into categories. Cats share a name, an age, and a color, so a program can describe all cats with the same set of properties and behaviors. 1:00
- When you define your own data type, you choose its attributes. Data attributes are the stored values that make up the object, and procedural attributes are the actions you can take on it, written as methods. 2:25
- The init method is always written first in a class. It tells Python how to create a new object, and the self parameter stands for the object being created. 4:15
- Writing self.age creates an instance variable. Because it starts with self, it stays around for as long as the object exists, unlike a normal variable that disappears when the method ends. 6:01
- Getters are simple methods that return the value of a data attribute, and setters let someone using the class change that value. 9:29
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
- A getter like get_age returns a data attribute's value, so it gives the same result as reading the attribute directly. The getter is the better style, because it does not expose how the class stores its information. 12:53
- Someone using a class should not need to know its internal parts, just as you do not need to know how a list is built inside to use it. The way the class works inside should stay hidden. 13:57
- If the class author renames a data attribute, code that reads it directly breaks, but code that uses the getter and setter still works. That is why methods are safer. 15:45
- The rule to follow is to use only getters and setters to change and work with objects. You should almost never touch data attributes directly. 16:08
- Python lets you change data attributes or add new ones from outside the class, but doing so breaks the consistency that makes a class useful. Only do these things inside the class definition. 18:16
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
- A dictionary stores entries by key. To add an entry, you use indexing: put the key in square brackets and set it equal to the value. There is no append or plus sign for dictionaries. 21:18
- Printing a whole dictionary whose values are animal objects does not show the ages. Python prints only the top level, so each animal shows up as a memory location instead of its details. 22:41
- To check the values, loop over the dictionary's items and print each value on its own. Python can print an animal object directly because the class has a str method that tells Python how to show it as text. 23:26
- To build a list by matching positions in two lists, loop over the index numbers with range(len(...)), not over the elements themselves. Then use the index to read from both lists. 26:47
- When you create an animal with only an age, its name starts as None. Use the setter function to set the name, instead of changing the attribute directly. Save the new object in a variable so you can use it later. 29:00
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
- A subclass is a more specific group inside a bigger group. Every cat, rabbit, and person is also an animal, so each one has an age and can do the things an animal can do. 33:52
- A subclass can add its own data attributes and behaviors. A cat might have a color or pattern, and a person might have a list of friends. 35:37
- A subclass can also change a behavior it inherits. This is called overriding. A student can have its own speak method that replaces the person's version. 36:23
- In code, you name the parent class in parentheses after the child class name, such as cat with animal in parentheses. This tells Python that a cat is an animal. 38:32
- If a child class has no init method of its own, Python uses the parent's init. That is why a cat is created the same way as an animal. 39:16
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
- Every class in Python is a child of the generic object class, so a cat is also a Python object. A child class copies its parent's init, data attributes, getters, setters, and str method. 40:00
- If a child class defines a method with the same name as one in its parent, the child's version is used. This is called overriding. A child can also add new behavior, such as cat's speak method. 40:46
- A method used on an object is looked for first in that object's own class. If it is not there, Python checks the parent, then the parent's parent, and so on up to object. If it is never found, Python gives an error. 43:53
- When a child needs a different setup, such as a person being created with a name and an age, it can write its own init. It can also call the parent's init to reuse the work already done there. 46:45
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
- A class can inherit from another class. Person inherits from Animal, so a Person can use get_name and get_age even though Person never defines them. 50:25
- A child class, also called a subclass, has everything its parent has and can do. It can also override a parent behavior, which means replacing it with its own version, and add new attributes or methods. 55:37
- A Student is a Person, so the Student's init method calls the Person's init method. That sets up the name, age, and friends in one line instead of repeating five lines. 57:02
- The Student class overrides the speak method from Person. Its version randomly picks one of four strings to print each time. 58:10
- A class variable is a shared resource. All objects of that class can use it, unlike instance variables, which hold different values for each object. 59:39
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
- A class variable is a plain variable written outside the methods of a class. Every instance of that class can read it and change it, and everyone sees the latest value. 1:00:00
- The lesson uses a class variable called tag, set to 1, as a counter. Each new rabbit takes the current tag as its own ID number, called rid, and then adds 1 to the tag before it finishes being created. 1:02:04
- Because the tag goes up by one each time, the first rabbit gets ID 1, the second gets ID 2, and the third gets ID 3. The line that adds 1 has to finish before the next rabbit can read the tag. 1:04:17
- The add method, written as a dunder method (a special method with double underscores in its name), lets you add two rabbits with the plus sign. The result is a new newborn rabbit with age 0, whose two parents are the rabbits you added. 1:07:57
- The lesson says not to write a setter for rid, because changing IDs by hand would break the counting. Class variables should be used only in specific situations, not for lots of shared values. 1:07:13
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
- When you add two rabbits with +, Python runs a special method behind the scenes and makes a new rabbit whose parents are the two rabbits you added. 1:10:00
- The new rabbit gets the next ID from a shared counter called tag, and the counter goes up by one. This happens even when the rabbit is made with +, so every rabbit is still counted. 1:11:00
- The lecturer's design choice is that two rabbits are equal if they have the same parents, even if the parents were added in the opposite order. 1:12:23
- The equality check compares the parents' IDs instead of the rabbit objects themselves. An earlier version crashed when one parent was None, while IDs are plain, unique numbers that are easy to compare. 1:13:29
- A class variable is shared by all the objects made from the class. If one object changes it, every other object sees the change. 1:16:07
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