Contact
Evil Genius SocietyHomeLearnForumLeaderboardSocialsMessages
← Back to module

Lesson 6 of 11

Inheritance lets one class take on everything another class has, then add or change parts. The original is the parent (or base class); the new one is the child (or subclass).

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return f"{self.name} makes a sound."

class Dog(Animal):
    pass

rex = Dog("Rex")
print(rex.name)
print(rex.speak())

class Dog(Animal): means "Dog is an Animal." Even though Dog's body is just pass, it got __init__ and speak() for free.

Why inherit?

Say you need dogs, cats, and birds. Without inheritance, every class repeats the same name handling. With it, shared behaviour lives once in Animal, and each child adds only what makes it different.

class Animal:
    def __init__(self, name):
        self.name = name

    def describe(self):
        return f"This is {self.name}."

class Dog(Animal):
    def fetch(self):
        return f"{self.name} fetches the ball!"

class Cat(Animal):
    def scratch(self):
        return f"{self.name} scratches the sofa."

rex = Dog("Rex")
whiskers = Cat("Whiskers")

print(rex.describe())
print(rex.fetch())
print(whiskers.describe())
print(whiskers.scratch())

Both children have describe(); only Dog has fetch(). Calling whiskers.fetch() raises AttributeError.

How Python finds a method

When you call rex.describe(), Python looks on Dog first. Not there, so it checks Animal. Found — it runs that one. This search order is called the method resolution order, and it's why a child can override anything simply by defining its own version (next lesson).

isinstance()

isinstance(obj, Class) is True for the class and any of its parents.

class Animal:
    pass

class Dog(Animal):
    pass

rex = Dog()
print(isinstance(rex, Dog))
print(isinstance(rex, Animal))
print(type(rex) is Animal)

isinstance respects inheritance; type() is does not. Prefer isinstance — a Dog genuinely is an Animal, and code expecting an animal should accept one.

Is-a, not has-a

Only inherit when the child truly is a kind of the parent. A Dog is an Animal; a SavingsAccount is a BankAccount. That works.

A Car is not an Engine — it has one. For that, store the engine as an attribute instead:

class Engine:
    def start(self):
        return "Vroom!"

class Car:
    def __init__(self):
        self.engine = Engine()

    def start(self):
        return self.engine.start()

print(Car().start())

Getting this backwards is the most common way inheritance goes wrong.

Try it yourself

Make Dog and Cat both inherit from Animal.

Checkpoint

  • class Child(Parent): inherits all the parent's attributes and methods.
  • Children add their own behaviour on top.
  • Python searches the child first, then walks up to the parent.
  • isinstance() respects inheritance; type() is doesn't.
  • Inherit for "is-a"; use an attribute for "has-a".

Questions about this lesson

Log in to ask a question.