new operator - Why isn't __new__ in Python new-style classes a class method? -



new operator - Why isn't __new__ in Python new-style classes a class method? -

the changelog python 2.2 (where new-style classes introduced) says next __new__ function:

__new__ static method, not class method. thought have class method, , that's why added classmethod primitive. unfortunately, class methods, upcalls don't work right in case, had create static method explicit class first argument.

however, cannot think of why class methods wouldn't work purpose, , better. why didn't __new__ end class method in end? guido refer when says "upcalls don't work right in case"?

__new__ beingness static method allows use-case when create instance of subclass in it:

return super(<currentclass>, cls).__new__(subcls, *args, **kwargs)

if new class method above written as:

return super(<currentclass>, cls).new(*args, **kwargs)

and there no place set subcls.

i don't see when proper utilize of __new__, though. maybe i'm not seeing it, seems me pathological utilize of (and should said, if still want it, access object.__new__.__func__). @ least, find hard imagine have been reason guido alter __new__ beingness class method static method.

a more mutual case phone call parent __new__ without using super(). you need place pass cls explicitly in case:

class base(object): @classmethod def new(cls): print("base.new(%r)" % (cls,)) homecoming cls() class usesuper(base): @classmethod def new(cls): print("usesuper.new(%r)" % (cls,)) homecoming super(usesuper, cls).new() # passes cls first arg class nosuper(base): @classmethod def new(cls): print("nosuper.new(%r)" % (cls,)) homecoming base.new() # passes base of operations first arg class usefunc(base): @classmethod def new(cls): print("usefunc.new(%r)" % (cls,)) homecoming base.new.im_func(cls) # or `.__func__(cls)`. # passes cls first arg print(usesuper.new()) print('-'*60) print(nosuper.new()) print('-'*60) print(usefunc.new())

python new-operator language-design new-style-class

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -