![](/img/trans.png)
[英]Python is there a difference calling a method from super, or from the class
[英]Python 2.3 calling super class method
在以下Python 2.3
腳本中調用基類函數時遇到問題。 在審閱這篇文章后:
我生成了這小段代碼:
class Base(object):
def func(self):
print "Base.func"
class Derived(Base):
def func(self):
super(Base, self).func()
print "Derived.func"
Derived().func()
上面的代碼生成此錯誤:
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'
我錯過了什么?
你應該給super
你想要升級的派生類,而不是基類:
super(Derived, self).func()
現在你正試圖訪問Base
的超類的func方法,它甚至可能不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.