Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using super() in a child class?
easy
A. To call a method from the parent class
B. To create a new instance of the child class
C. To delete the parent class
D. To override the child class method completely

Solution

  1. Step 1: Understand what super() does

    super() is used to access methods from the parent class inside a child class.
  2. Step 2: Identify the correct purpose

    Calling a parent class method helps reuse code and extend functionality.
  3. Final Answer:

    To call a method from the parent class -> Option A
  4. Quick Check:

    super() calls parent method = A [OK]
Hint: Remember: super() means 'call parent method' [OK]
Common Mistakes:
  • Thinking super() creates new objects
  • Believing super() deletes classes
  • Assuming super() overrides child methods fully
2. Which of the following is the correct syntax to call a parent class method greet inside a child class method using super()?
easy
A. super().greet()
B. super->greet()
C. super[greet]()
D. super.greet()

Solution

  1. Step 1: Recall the syntax of super()

    The correct way to call a parent method is using super() followed by dot and method name.
  2. Step 2: Match the correct option

    Only super().greet() uses the right parentheses and dot notation.
  3. Final Answer:

    super().greet() -> Option A
  4. Quick Check:

    Use parentheses with super() = D [OK]
Hint: super() always needs parentheses before method call [OK]
Common Mistakes:
  • Omitting parentheses after super
  • Using square brackets instead of parentheses
  • Using arrow notation which is invalid in Python
3. What will be the output of this code?
class Parent:
    def greet(self):
        return "Hello from Parent"

class Child(Parent):
    def greet(self):
        return super().greet() + " and Child"

c = Child()
print(c.greet())
medium
A. Hello from Parent
B. Hello from Child
C. Hello from Parent and Child
D. Error: super() not used correctly

Solution

  1. Step 1: Understand method calls in Child.greet()

    Child's greet calls super().greet() which runs Parent's greet returning Hello from Parent.
  2. Step 2: Combine returned strings

    Child's greet adds and Child to the parent's string, so final output is Hello from Parent and Child.
  3. Final Answer:

    Hello from Parent and Child -> Option C
  4. Quick Check:

    super() calls parent method + extra text = A [OK]
Hint: super() returns parent result; child can add more [OK]
Common Mistakes:
  • Expecting only parent's message without child addition
  • Thinking super() causes error here
  • Ignoring the string concatenation
4. Find the error in this code using super():
class Base:
    def show(self):
        print("Base show")

class Derived(Base):
    def show(self):
        super.show()
        print("Derived show")

d = Derived()
d.show()
medium
A. Derived class should not override show()
B. Base class method show() is missing
C. print statements are incorrect
D. super.show() should be super().show()

Solution

  1. Step 1: Identify how super() is called

    The code uses super.show() which is invalid syntax; super must be called as a function.
  2. Step 2: Correct the syntax

    It should be super().show() to properly call the parent method.
  3. Final Answer:

    super.show() should be super().show() -> Option D
  4. Quick Check:

    super() needs parentheses before method = C [OK]
Hint: Always use super() with parentheses before method call [OK]
Common Mistakes:
  • Calling super without parentheses
  • Thinking parent method is missing
  • Believing overriding is not allowed
5. You want to extend a parent class __init__ method to add a new attribute in the child class. Which code correctly uses super() to do this?
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        ???
        self.breed = breed
Choose the correct replacement for ???.
hard
A. super(Dog, self).__init__(breed)
B. super().__init__(name)
C. Animal.__init__(self, breed)
D. super().__init__(breed)

Solution

  1. Step 1: Understand parent __init__ parameters

    Animal's __init__ takes name, so we must pass name to it.
  2. Step 2: Use super() correctly in child __init__

    Calling super().__init__(name) runs Animal's __init__ properly, then child adds breed.
  3. Final Answer:

    super().__init__(name) -> Option B
  4. Quick Check:

    super() calls parent with correct args = B [OK]
Hint: Pass parent's expected args to super().__init__() [OK]
Common Mistakes:
  • Passing wrong argument to super()
  • Calling parent __init__ without self
  • Using old super() syntax incorrectly