HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
So far we have created a child class that inherits the properties and methods from its parent.
We want to add the __init__()
function to the child class (instead of the pass
keyword).
Note: The __init__()
function is called automatically every time the class is being used to create a new object.
Add the __init__()
function to the
Student
class:
class Student(Person):
def __init__(self, fname, lname):
#add properties etc.
When you add the __init__()
function, the child class will no longer inherit
the parent's __init__()
function.
Note: The child's __init__()
function overrides the inheritance of the parent's
__init__()
function.
To keep the inheritance of the parent's __init__()
function, add a call to the
parent's __init__()
function:
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
Now we have successfully added the __init__() function, and kept the
inheritance of the parent class, and we are ready to add functionality in the
__init__()
function.