class Works(type):
def __new__(cls, *args, **kwargs):
print([cls,args]) # outputs [<class '__main__.Works'>, ()]
return super().__new__(cls, args)
class DoesNotWork(type):
def __new__(*args, **kwargs):
print([args[0],args[:0]]) # outputs [<class '__main__.doesNotWork'>, ()]
return super().__new__(args[0], args[:0])
Works() # is fine
DoesNotWork() # gets "RuntimeError: super(): no arguments"
As far as I can see, in both cases super._new__ receives the class literal as first argument, and an empty tuple as the 2nd.
So why does one give an error and the other not?
cls
from__new__
in yourDoesNotWork
... (also - you really should be using*args
insuper()
as well to keep signatures correct...__new__
inDoesNotWork
a concrete 1st argument, but the argumentssuper()
is being passed are identical, so I dont understand the error. Using *args gives meTypeError: type() takes 1 or 3 arguments
error though thats a seperate question.__new__
and not__init__
- it looks like you're really trying to use the later...super(cls)
is the same assuper(cls, the_first_argument_passed_to_this_function)
. I suspect something similar is happening here -super()
is trying to default tosuper(first_argument_passed_to_this_function)
, but*args
and**kwargs
don't count as a arguments (because they're a special case), so it fails.