什麼是錯誤使用以下代碼?Python:使用靜態方法的嵌套類失敗
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中使用它。 – 2013-04-25 08:52:17
_「如果你改變超類的名字(即A),那麼你將不得不更新所有使用內部自身作爲A」_ - **這不是什麼_super class_意味着** – Eric 2013-04-25 21:02:30