Unit 13: Families of Objects
Passing abilities down.
Unit 13 of 31 in Python for kids. Its 5 lessons are Passing Things Down, Calling the Parent, Changing Your Mind, Same Word, Different Action and Family Master — below is everything each one explains, and a question or two from it to try.
Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
👨👩👧 Passing Things Down
One class built from another
Put another class in the brackets and yours starts with everything that one had. Dog is an Animal, so it gets name and eat without writing them again.
Python
class Animal:
def __init__(self, name):
self.name = name
def eat(self):
print(self.name + " is eating")
class Dog(Animal):
pass
Dog("Rex").eat()
It prints
Rex is eating
Adding something of your own
A child class can add abilities the parent never had. The parent stays exactly as it was.
Python
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def fetch(self):
print(self.name + " fetches the ball")
Dog("Rex").fetch()
It prints
Rex fetches the ball
Asking what something is
isinstance checks whether an object belongs to a class — and a Dog counts as an Animal too, because that is what inheriting means.
Python
class Animal:
pass
class Dog(Animal):
pass
rex = Dog()
print(isinstance(rex, Dog))
print(isinstance(rex, Animal))
It prints
True True
Try it yourself
What does class Dog(Animal): mean?
- Dog contains an Animal
- Dog starts with everything Animal has
- Dog replaces Animal
- Animal is a parameter
What does this print?
Python
class Vehicle:
def move(self):
print("moving")
class Bike(Vehicle):
pass
Bike().move()
Answer them in the app
📞 Calling the Parent
When the child needs more information
If the child writes its own __init__, the parent's no longer runs. super().__init__(...) calls it yourself, so the parent still does its part.
Python
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
rex = Dog("Rex", "collie")
print(rex.name, rex.breed)
It prints
Rex collie
Forget it and things go missing
Without the super() call, the parent never gets to store name — and reaching for it later fails. This is one of the most common bugs in classes.
Python
class Animal:
def __init__(self, name):
self.name = name
class Cat(Animal):
def __init__(self, name):
self.lives = 9
c = Cat("Tig")
print(hasattr(c, "name"))
It prints
False
Try it yourself
What does super() mean?
- The best version
- The class this one was built from
- A very fast method
- The object itself
What does this print?
Python
class Base:
def __init__(self):
self.n = 1
class Child(Base):
def __init__(self):
super().__init__()
self.n = self.n + 5
print(Child().n)
Answer them in the app
🔀 Changing Your Mind
Replacing a method you inherited
Write a method with the same name as the parent's and yours wins. That is called overriding.
Python
class Animal:
def speak(self):
print("...")
class Dog(Animal):
def speak(self):
print("Woof")
Dog().speak()
Animal().speak()
It prints
Woof ...
Adding to it instead of replacing
super() works in any method, not just __init__. Use it when you want the parent's behaviour *and* a bit more.
Python
class Animal:
def speak(self):
print("I am an animal")
class Dog(Animal):
def speak(self):
super().speak()
print("and I say woof")
Dog().speak()
It prints
I am an animal and I say woof
Try it yourself
What does this print?
Python
class A:
def hi(self):
print("A")
class B(A):
def hi(self):
print("B")
super().hi()
B().hi()
A child writes a method with the same name as its parent. What happens?
- Python complains
- Both run, parent first
- The child's version is the one that runs
- The parent wins
Answer them in the app
🎭 Same Word, Different Action
One loop, many kinds of thing
This is polymorphism: the loop does not care what each animal is. It just says speak(), and each one answers in its own way.
Python
class Animal:
def speak(self):
print("...")
class Dog(Animal):
def speak(self):
print("Woof")
class Cat(Animal):
def speak(self):
print("Meow")
for animal in [Dog(), Cat(), Animal()]:
animal.speak()
It prints
Woof Meow ...
It works with built-in things too
len behaves the same way — it asks the thing how long it is, and lists, text and dictionaries each answer differently.
Python
for thing in ["hello", [1, 2, 3], {"a": 1}]:
print(len(thing))
It prints
5 3 1
Try it yourself
Why is polymorphism useful?
- It makes code shorter to type
- You can add a new kind of animal without changing the loop at all
- It runs faster
- It saves memory
What does this print?
Python
class Shape:
def area(self):
return 0
class Square(Shape):
def __init__(self, side):
self.side = side
def area(self):
return self.side * self.side
for s in [Square(3), Shape()]:
print(s.area())
Answer them in the app
🏆 Family Master
Try it yourself
What does this print?
Python
class A:
def go(self):
print("A goes")
class B(A):
pass
class C(B):
def go(self):
print("C goes")
B().go()
C().go()
Answer it in the app