Call a parent class's method from child class in Python?


Question

When creating a simple object hierarchy in Python, I'd like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for this (super). In Perl, I might 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 appears that I have to name the parent class explicitly from the child. In the example above, I'd have to do something like Foo::frotz().

This doesn't seem right, since this behavior makes it hard to make deep hierarchies. If children need to know what class defined an inherited method, then all sorts of information pain is created.

Is this an actual limitation in python, a gap in my understanding or both?

1
530
9/17/2018 11:46:44 AM

Accepted Answer

Yes, but only with new-style classes. Use the super() function:

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

For Python 3.x, you can use:

class Foo(Bar):
    def baz(self, arg):
        return super().baz(arg)
635
6/12/2019 10:23:02 AM

Python also has super as well:

super(type[, object-or-type])

Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.

Example:

class A(object):     # deriving from 'object' declares A as a 'new-style-class'
    def foo(self):
        print "foo"

class B(A):
    def foo(self):
        super(B, self).foo()   # calls 'A.foo()'

myB = B()
myB.foo()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon