In this lesson, you’ll learn how inheritance is used to write maintainable Python code that avoids redundancy.
Inheritance allows one class to subclass another. If we want to make the Baby
class a child class of the Person
class, we can define Baby
like this:
class Baby(Person):
# Baby code here
By default, any Baby
object we create will inherit its attributes and methods from its parent. This means it will have the same attributes and members that the parent class defined.
In the lesson, you’ll see how you can override a method so that the Baby
class can contain a different implementation for that method. You’ll also learn how to extend the functionality of the Baby
class by defining new methods, which will be exclusive to only Baby
objects.
The isinstance()
function is used to determine if an object is of a specific type. It takes 2 arguments: the object to be checked, and a type to check against. It supports inheritance, so in the example of our Baby
and Person
objects, isinstance(baby, Person)
will return True
because our Baby
class is the child class of Person
, and so we can say our baby
object is a Person too.
Peter T on March 22, 2019
In the above text, please change exampole to example.