2009-08-31 63 views
0

我有一段時間在Python中看到的最奇怪的錯誤(版本3.0)。如何在Python 3.0中調用super()?

更改函數的簽名會影響super()是否有效,儘管它沒有參數。你能解釋爲什麼發生這種情況嗎

感謝,

克里斯

>>> class tmp: 
...  def __new__(*args): 
...    super() 
... 
>>> tmp() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 3, in __new__ 
SystemError: super(): no arguments 
>>> class tmp: 
...  def __new__(mcl,*args): 
...    super() 
... 
>>> tmp() 
>>> 

回答

6

由於the docs說,「零參數表單自動搜索堆棧幀的類(__class__)和第一個參數。」你的__new__的第一個例子沒有第一個參數 - 它聲稱可以用零個或多個參數調用它,所以沒有爭議的super被難住了。你的第二個例子有明確的第一個參數,所以堆棧框架中的搜索成功。

1

蟒蛇3.0新的超級試圖動態地做出選擇。在這裏,閱讀PEP here應該解釋一切。

相關問題