Course 1.2Lesson 1 of 5
Inheritance builds one class on another. A child class, written with the parent in brackets, gets all the attributes and methods of the parent and can add its own, so the shared code is written once. isinstance asks whether an object is of a class or of a class built on it, and issubclass asks about two classes. A child that needs more data writes its own __init__ and calls the parent set-up with super, and if it forgets, the attributes of the parent are missing. Use inheritance only when the child really is a kind of the parent.
Try the code and the simulations beside the lesson.
After this lesson you can
isinstance and issubclass, and__init__ with super().A dog is an animal. A cat is an animal. All animals have a name and can eat. Dogs can also fetch, and cats can also climb. If you write a class for each kind, you would write name and eat again and again. There is a better way: write what all animals share once, in a class Animal, and build Dog and Cat on it. This is called inheritance.
Predict first. Dog has no __init__ and no eat. Will Dog("Rex").eat() work? Then run it.
class Dog(Animal): writes the parent in brackets. Animal is the parent (or base class, or superclass). Dog is the child (or subclass).Dog gets everything that Animal has. Dog("Rex") uses the inherited __init__, and sets the name. rex.eat() and rex.speak() are inherited too.fetch is new: only dogs have it. A child adds to the parent.isinstance(rex, Dog) is True. isinstance(rex, Animal) is also True: a dog is an animal. isinstance(Animal("x"), Dog) is False: not every animal is a dog. The relation goes one way.issubclass(Dog, Animal) asks the same about two classes.Dog.__mro__ is the list of classes that Python searches, in order, for a method: first Dog, then Animal, then object, the class under every class.So the rule: Python looks for a method or attribute in the object, then in its class, and then up through the parents.
Suppose that a Dog needs more data: a list of tricks. It writes its own __init__. But then the __init__ of Animal is not run, unless the child asks for it. The tool is super(): "the parent".
What if the child forgets?
Animal fixes every child.Animal works for every kind of animal, as the next lesson shows.Both objects answer isinstance(shape, Shape) with True: a square is a shape. That is the test for inheritance: "is a".
Inheritance says "is a". A Dog is an Animal. But a Car is not an Engine. A car has an engine. If the "is a" sentence sounds wrong, do not use inheritance. You will see the other tool, composition, in the fourth lesson.
super().__init__ in a child with its own __init__.__init__.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
isinstance on a small family.name.class Child(Parent): builds a child on a parent. The child gets everything and adds its own.isinstance(x, Class) is true for the class and for the classes built on it.__init__ calls super().__init__(...).Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
class Dog(Animal): mean?isinstance(Dog("Rex"), Animal) true or false?__init__ and does not call super().__init__?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.