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…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Refer to Base Class in Python." Ariessa Norramli | Sciencx - Sunday February 28, 2021, https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/
HARVARD
Ariessa Norramli | Sciencx Sunday February 28, 2021 » How to Refer to Base Class in Python., viewed ,<https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Refer to Base Class in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/
CHICAGO
" » How to Refer to Base Class in Python." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/
IEEE
" » How to Refer to Base Class in Python." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/28/how-to-refer-to-base-class-in-python/. [Accessed: ]
rf:citation
» How to Refer to Base Class in Python | Ariessa Norramli | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.