简体   繁体   English

Python 2.3调用超类方法

[英]Python 2.3 calling super class method

having trouble calling base class function in the following Python 2.3 script. 在以下Python 2.3脚本中调用基类函数时遇到问题。 after reviewing this post: 在审阅这篇文章后:

Call a parent class's method from child class in Python? 从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你想要升级的派生类,而不是基类:

super(Derived, self).func()

Right now you are trying to access the func method of Base 's superclass, which may not even exist. 现在你正试图访问Base的超类的func方法,它甚至可能不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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