【发布时间】:2013-04-25 08:40:21
【问题描述】:
以下代码有什么错误?
class A:
def A_M(self): pass
class B:
@staticmethod
def C(): super(B).A_M()
错误(Python 2.7.3):
>>> a = A()
>>> a.B.C()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "..x.py", line 36, in C
def C(): super(B).A_M()
NameError: global name 'B' is not defined
编辑:
解决方案很简单:
class A:
def A_M(self): pass
class B:
@staticmethod
def C(): A().A_M() #use of A() instead of supper, etc.
重要提示,此解决方案存在问题。如果您更改超类的名称(即A),那么您必须将其内部的所有用途更新为A :))。
【问题讨论】:
-
为什么你认为你想要一个嵌套类?在 Python 中使用它的理由几乎为零。
-
" 如果您更改超类的名称(即 A),那么您将必须将其内部的所有用途更新为 A" - 这不是 超类意味着
标签: python class python-2.7 super