简体   繁体   中英

Python 2.3 calling super class method

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM