python

How to Refer to Base Class in Python


In this article, you will learn how to refer to base class in Python.

Let’s say you have 2 classes, a base class named Father and a derived class named Son.

class Father(object):
def __init__(self, fatherGender):
print(fatherGender, 'is a male.')

class Son(Father):
def __init__(self):
print('Son is 20 years old.')

Refer to Base Class in Python

In order to refer to base class, you can use the super() method.

class Father(object):
def __init__(self, fatherGender):
print(fatherGender, 'is a male.')

class Son(Father):
def __init__(self):
print('Son is 20 years old.')
super().__init__('Son')

object = Son()
# Son is 20 years old.
# Son is a male.

Note: The super() method functions by referring to the base class in single inheritance.


Share on social media

//