Complex Relationships3:37 with Kenneth Love
Classes are great, but they're even better with parents.
Inheritance in Python is both easier and harder than in some other languages. Python's inheritance usually works exactly how you expect it to. The child classes, or subclasses, get all of the attributes and methods of their parents and grandparents and so on. But thanks to things like the "method resolution order (MRO)", which we'll talk about later, sometimes it's not that simple. But let's not worry about that just yet! Make sure you understand these terms before moving on:
- Parent or Super class: the class that a class inherits from. These can go on for a long way, too, so be sure to consider grandparent and great-grandparent classes, too.
All classes have the ultimate ancestor of
- Child or Sub class: the class that inherits from a particular class.
class Symbol: pass class Letter(Symbol): pass class Alpha(Letter): pass
Letter has two superclasses:
Symbol inherits from
object even though we didn't explicitly state it).
Letter has one subclass,
An instance of the class
Alpha could use any attributes or methods that were defined on
Letter. Those two classes, though, wouldn't have access to attributes or methods that belonged to
Alpha. Inheritance is a one-way street.
You need to sign up for Treehouse in order to download course files.Sign up