Remember
super() delegates attribute lookup along the method resolution order. It supports cooperative inheritance rather than naming one fixed parent.
super() delegates attribute lookup along the method resolution order. It supports cooperative inheritance rather than naming one fixed parent.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Parent:
def greeting(self):
return "Hello"
class Child(Parent):
def greeting(self):
return super().greeting() + ", " + 'Ada'
print(Child().greeting())Top-level scope
class ParentObject 4 · typeclass Childsuper() delegates attribute lookup along the method resolution order. It supports cooperative inheritance rather than naming one fixed parent.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Parent:
def greeting(self):
return "Hello"
class Child(Parent):
def greeting(self):
return super().greeting() + ", " + 'Ada'
print(Child().greeting())
|
v
DISPLAYED RESULT
Hello, Ada
FINAL BINDINGS
Parent --> object 2 (type)
Child --> object 4 (type)
object 2: class Parent
object 4: class ChildChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Parent:
def greeting(self):
return "Hello"
class Child(Parent):
def greeting(self):
return super().greeting() + ", " + 'Ada'
print(Child().greeting())Top-level scope
class ParentObject 4 · typeclass ChildThe example has finished. Compare the final output with the values and references above.
Hello, Ada
What do you expect this program to print?
class Parent:
def greeting(self):
return "Hello"
class Child(Parent):
def greeting(self):
return super().greeting() + ", " + 'Ada'
print(Child().greeting())super() FunctionThe super() function in Python is used to call methods from a parent or superclass. It's commonly used in inheritance to avoid repeating code and make child classes more manageable.
super().method_name(arguments)
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
super().greet()
print("Hello from Child")
c = Child()
c.greet()
Hello from Parent
Hello from Child
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
d = Dog("Buddy", "Labrador")
print(d.name)
print(d.breed)
Buddy
Labrador
class A:
def process(self):
print("A.process()")
class B(A):
def process(self):
print("B.process()")
super().process()
class C(B):
def process(self):
print("C.process()")
super().process()
c = C()
c.process()
C.process()
B.process()
A.process()
super() outside a class method will raise an error.super().__init__() in child constructors to properly initialize inherited attributes.super() is frequently tested in OOP interviews to check your understanding of inheritance and code reuse. You may be asked to rewrite code using super() to make it cleaner.
super() helps you access parent class methods.Create a class Vehicle with a method start(). Then create a class Car that inherits from Vehicle and overrides start(), but still calls Vehicle.start() using super().
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
def start(self):
super().start()
print("Car started")
c = Car()
c.start()
Vehicle started
Car started
Connect what you noticed in the example with the idea below.
super() delegates attribute lookup along the method resolution order. It supports cooperative inheritance rather than naming one fixed parent.
super() always means the first base class regardless of method resolution order.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.