This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
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.
The post How to Refer to Base Class in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-28T17:21:57+00:00) How to Refer to Base Class in Python. Retrieved from https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.