how to use parent class method in child class in python code example
Example 1: how to access parent class attribute from child class in python
class Person:
def __init__(self, fname, lname):
self.fname = fname
self.lname = lname
def display(self):
'''To check if parent method works'''
print(self.fname)
print(self.lname)
class Subscriber(Person):
def __init__(self, fname, lname, ID):
self.ID = ID
Person.__init__(self, fname, lname)
def displayID(self):
'''To see if child method works'''
print(self.ID)
John = Subscriber("John", "Doe", 1)
John.display()
John.displayID()
Example 2: how to get the parent class using super python
class Foo(Bar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)