Casebash
Casebash

Reputation: 119042

How to call super() in Python 3.0?

I have the strangest error I have seen for a while in Python (version 3.0).

Changing the signature of the function affects whether super() works, despite the fact that it takes no arguments. Can you explain why this occurs?

Thanks,

Chris

>>> 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()
>>>

Upvotes: 0

Views: 935

Answers (2)

Alex Martelli
Alex Martelli

Reputation: 882851

As the docs say, "The zero argument form automatically searches the stack frame for the class (__class__) and the first argument." Your first example of __new__ doesn't HAVE a first argument - it claims it can be called with zero or more arguments, so argumentless super is stumped. Your second example DOES have an explicit first argument, so the search in the stack frame succeeds.

Upvotes: 6

Chmouel Boudjnah
Chmouel Boudjnah

Reputation: 2559

python 3.0 new super is trying to dynamically make a choice for you here, read this PEP here that should explain everything.

Upvotes: 1

Related Questions