Start learning free

LESSON 38 OF 68 · OBJECT-ORIENTED PROGRAMMING

Calling the Parent with super() in Python

Practice Read the full explanation

TL ; DR:

Inside an override, super() gives you the parent class, so super().bark() runs the parent's version of bark on the same object. This lets you extend the parent's behavior instead of replacing it.


class Puppy(Dog):
    def bark(self):
        super().bark()    # do everything the parent did
        print("Yip! Said " + self.name)

Why it matters

Overriding a method replaces the parent's behavior. Often you want to extend it instead: keep what the parent did and add something more. A typical example is a save method: the parent writes the row to the database, and a subclass runs the parent's save first and then writes a log line.

Your task

The Puppy class already overrides bark, but the body is empty. Inside Puppy.bark(self):

  1. call the parent's version with super().bark(), so Woof! I am <name> still prints;
  2. then print("Yip! Said " + self.name).

Notes:

  • Write super().bark() with the parentheses: super() is a call that returns a stand-in for the parent class.
  • self.name is the name stored by Dog.__init__; Puppy has no __init__ of its own, so it inherits the parent's.

The driver (already written, it runs when you press Run):

buddy = Puppy("Buddy")
buddy.bark()

rex = Puppy("Rex")
rex.bark()
dog = Dog("Max")
dog.bark()
  • buddy is a Puppy: its bark prints the parent's line, then its own.
  • rex is another Puppy: the names come from the object, nothing is hard-coded.
  • dog is a plain Dog: its bark is unchanged and prints only the parent's line, with no Yip!.

Expected output:

Woof! I am Buddy
Yip! Said Buddy
Woof! I am Rex
Yip! Said Rex
Woof! I am Max
STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · OBJECT-ORIENTED PROGRAMMING

    Base Keyword in Python, explained

    Problem overview

    The lesson has two classes. Dog stores a name in __init__ and has a bark method that prints Woof! I am followed by the name. Puppy is a subclass of Dog with its own bark, whose body is empty. Your job is to fill it in so a puppy first does everything a dog does when it barks, and then prints one more line, Yip! Said followed by its name.

    The driver creates two puppies, Buddy and Rex, and a plain dog, Max. A puppy's bark prints two lines, and Max's prints only the first one.

    Extending a method instead of replacing it

    When a subclass defines a method with the same name as one in its parent, the subclass version replaces the parent's for objects of the subclass. That is called overriding. Often replacing is too much: you want to keep what the parent did and add something. For that, the override calls the parent's version itself.

    In Python, super() does this. It returns a stand-in for the parent class, so super().bark() runs Dog.bark on the current object. Inside it, self is still the puppy, which is why self.name works: Puppy has no __init__ of its own, so it inherits the one from Dog, and that one stored the name.

    Think of an override that calls super() as a sandwich. Code before the call runs first, the parent's version runs in the middle, and code after the call runs last. Which side you put your own code on decides the output order, and this lesson asks for the parent's line first.

    What super() looks up

    Every class knows its parent, and super() uses that to find the next class up the chain that has the method you ask for. Here the chain is Puppy, then Dog, then object, the class every Python class ends with. So super().bark() inside Puppy lands on Dog.bark. The call runs on the same object: no second dog is created, and self.name is the name that was given when the puppy was made.

    The same tool works in constructors. A subclass that needs an extra attribute can write its own __init__, call super().__init__(name) so the parent still sets self.name, and then add its own fields. This lesson doesn't need that, because Puppy only changes bark.

    Step by step

    1. Call the parent's bark first. Write super().bark() with both pairs of parentheses. super() is a call that produces the parent stand-in, and .bark() is a call on it. Without this line the puppy would never print the Woof line.
    2. Add the puppy's own line. Print "Yip! Said " joined with self.name. Because it comes after the parent call, the output order is the parent's line and then the puppy's.

    The solution in Python

    class Dog:
        def __init__(self, name):
            self.name = name
        def bark(self):
            print("Woof! I am " + self.name)
    
    class Puppy(Dog):
        def bark(self):
            super().bark()
            print("Yip! Said " + self.name)
    
    buddy = Puppy("Buddy")
    buddy.bark()
    
    rex = Puppy("Rex")
    rex.bark()
    dog = Dog("Max")
    dog.bark()
    

    Notice that the driver at the bottom is part of the lesson's file. You only write the two lines inside Puppy.bark.

    What the output shows

    The expected output is five lines:

    Woof! I am Buddy
    Yip! Said Buddy
    Woof! I am Rex
    Yip! Said Rex
    Woof! I am Max
    

    The names come from the objects, nothing is hard-coded in the class. Max is a Dog, so his bark is the original and never prints a Yip! line.

    Edge cases and common mistakes

    • Forgetting the parentheses. super.bark() without () raises AttributeError: type object 'super' has no attribute 'bark', because super is the built-in itself and not the stand-in it returns. Write super().bark().
    • Passing self again. super().bark(self) raises TypeError: Dog.bark() takes 1 positional argument but 2 were given: super() already binds the method to the current object, so bark receives self by itself.
    • Leaving out the call. If the override only prints its own line, the parent's behavior is replaced, not extended.
    • The order of the lines. The parent's version can also run after your own code, or in the middle of it. With this lesson's order, the parent's line prints first. If the print comes before super().bark(), the same two lines appear the other way round: Yip! Said Pip and then Woof! I am Pip.
    • Calling the parent by name. Dog.bark(self) also works and prints the same line. super() is preferred because it doesn't repeat the parent's name, so it keeps working if the parent class changes.

    Trying it yourself

    A few checks you can add below the driver in the editor above:

    • isinstance(Puppy("Zed"), Dog) gives True: a puppy is a dog, so it can be used wherever a dog is expected.
    • Dog.bark(Puppy("Zed")) prints Woof! I am Zed only: it calls the parent's version directly and skips the puppy's override.
    • Giving Puppy a new bark that prints only its own line removes the Woof line from every puppy, while Max keeps his.

    Key takeaways

    • Overriding replaces a parent's method; calling super().method() inside the override extends it.
    • A subclass without its own __init__ uses the parent's, so attributes such as self.name are there.
    • Methods of the parent class are unaffected: a plain Dog still barks the old way.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free