0
0
Pythonprogramming~10 mins

Super function usage in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Super function usage
Define Base Class
Define Derived Class
Call Derived Method
Inside Derived: call super()
Execute Base Class Method
Return to Derived Method
Finish Derived Method
Shows how a derived class method calls its base class method using super(), then continues execution.
Execution Sample
Python
class A:
    def greet(self):
        print('Hello from A')

class B(A):
    def greet(self):
        super().greet()
        print('Hello from B')

b = B()
b.greet()
A derived class B calls the greet method of its base class A using super(), then prints its own message.
Execution Table
StepActionEvaluationOutput
1Create instance b of class Bb is B instance
2Call b.greet()Calls B.greet()
3Inside B.greet(), call super().greet()Calls A.greet()
4Execute A.greet()Print 'Hello from A'Hello from A
5Return to B.greet()Print 'Hello from B'Hello from B
6Finish B.greet()Method ends
💡 Method B.greet() finishes after calling base method and printing its own message
Variable Tracker
VariableStartAfter 1After 2Final
bundefinedB instance createdB instanceB instance
Key Moments - 2 Insights
Why do we use super().greet() inside the derived class method?
Using super().greet() calls the base class method so we can reuse its code before adding more behavior, as shown in execution_table step 3 and 4.
Does the base class method replace the derived class method?
No, the derived method calls the base method using super(), then continues its own code, as seen in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 4?
A'Hello from A'
B'Hello from B'
CNothing
DError
💡 Hint
Check the Output column at step 4 in the execution_table
At which step does the derived class method print its own message?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the Action and Output columns in execution_table for when 'Hello from B' is printed
If we remove super().greet() call, what changes in the output?
AOnly 'Hello from A' is printed
BBoth messages are printed as before
COnly 'Hello from B' is printed
DNo output is printed
💡 Hint
Consider that super().greet() triggers 'Hello from A' print in step 4
Concept Snapshot
Use super() in a derived class method to call the base class method.
Syntax: super().method_name()
This lets you reuse base code and add new behavior.
The base method runs first, then derived code continues.
Without super(), base method is not called.
Full Transcript
This visual execution shows how the super() function works in Python. We have a base class A with a greet method that prints a message. The derived class B overrides greet but calls super().greet() to run the base method first. Then B prints its own message. The execution table traces each step: creating an instance, calling B.greet(), calling A.greet() via super(), printing messages, and finishing. The variable tracker shows the instance b stays the same. Key moments clarify why super() is used and that the base method does not replace the derived one. The quiz tests understanding of output at each step and effects of removing super(). The snapshot summarizes the usage and behavior of super() in simple terms.