How to Call Parent Class Method in Python

How do I call a parent class’s method from a child class in Python?

When creating a simple object hierarchy in Python, I want to be able to invoke methods of the parent class from a derived class. In other languages like Perl and Java, there is a keyword (super) for this. For instance, in Perl, I would do this:

package Foo;

sub frotz {
    return "Bamf";
}

package Bar;
@ISA = qw(Foo);

sub frotz {
   my $str = SUPER::frotz();
   return uc($str);
}

In Python, it seems like I have to explicitly name the parent class from the child class. In the example above, I would have to use something like Foo.frotz(). This doesn’t seem ideal, as it can complicate deep hierarchies. If children need to know which class defined an inherited method, it creates unnecessary complexity.

Is this a limitation in Python, a gap in my understanding, or both? How can I use the Python call super method correctly in a more flexible way?

Ah, great question! When you want to call a parent class’s method in Python, the super() function is your best friend. It’s clean, elegant, and avoids the mess of explicitly naming parent classes. Here’s how you can do it:

class Bar:  
    def baz(self):  
        return "Hello from Bar!"  

class Foo(Bar):  
    def baz(self):  
        return super().baz() + " And hello from Foo!"  

This ensures the parent class’s method is invoked without hardcoding its name. Also, for Python versions earlier than 3, you need to use super() with a slightly different syntax:

class Foo(Bar):  
    def baz(self, arg):  
        return super(Foo, self).baz(arg)  

By using super(), you make your code more adaptable and future-proof. And yes, this is the preferred way to handle the python call super method!

Interesting topic! Another way to call the parent class’s method is by directly referencing the parent class, though it’s not as flexible as super(). Here’s how it works:

class Bar:  
    def baz(self):  
        return "Hello from Bar!"  

class Foo(Bar):  
    def baz(self):  
        return Bar.baz(self) + " And hello from Foo!"  

While this works, it tightly couples your child class to the parent class name, which can become a problem in complex hierarchies or multiple inheritance. This approach is fine for simple cases but lacks the elegance of super() in managing the python call super method.

As someone who’s worked with Python and multiple inheritance scenarios, I’ve found super() to be especially powerful. It’s not just about calling the parent’s method—it also ensures the method resolution order (MRO) is followed, which is crucial for multiple inheritance.

Here’s how you can use it effectively:

class Bar:  
    def baz(self):  
        return "Hello from Bar!"  

class Baz:  
    def baz(self):  
        return "Hello from Baz!"  

class Foo(Bar, Baz):  
    def baz(self):  
        return super(Foo, self).baz() + " And hello from Foo!"  

In this example, the MRO ensures Bar’s baz method is called first, as per Python’s rules. Using super() makes your code much more adaptable and maintains the flexibility required in object-oriented design. Definitely stick to super() for handling the python call super method in these cases.