Question

having trouble calling base class function in the following Python 2.3 script. after reviewing this post:

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

I've generate this small piece of code:

class Base(object):

    def func(self):
        print "Base.func"

class Derived(Base):

    def func(self):
        super(Base, self).func()
        print "Derived.func"

Derived().func()

code above generates this error:

Traceback (most recent call last):
  File "py.py", line 13, in ?
    Derived().func()
  File "py.py", line 10, in func
    super(Base, self).func()
AttributeError: 'super' object has no attribute 'func'

What am I missing?

Was it helpful?

Solution

You should give super the derived class from which you want to step up, not the base class:

super(Derived, self).func()

Right now you are trying to access the func method of Base's superclass, which may not even exist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top