Junior
How to refer to the parent class in a class?
sobes.tech AI
Answer from AI
The super() function is used to refer to the parent class.
class Parent:
def __init__(self, value):
self.value = value
class Child(Parent):
def __init__(self, value, extra_value):
super().__init__(value) # Call the parent class's __init__
self.extra_value = extra_value
def display(self):
print(f"Parent value: {self.value}")
print(f"Child extra value: {self.extra_value}")
super().display_parent_method() # Call the parent class's method
def display_parent_method(self):
print("This is a method from the parent class.")