__new__
静的メソッドであるため、サブクラスのインスタンスを作成するときにユースケースが可能になります。
return super(<currentclass>, cls).__new__(subcls, *args, **kwargs)
new
がクラス メソッドの場合、上記は次のように記述されます。
return super(<currentclass>, cls).new(*args, **kwargs)
置く場所がありませんsubcls
。
ただし、それがいつ適切な使用になるかはわかりません__new__
。多分私はそれを見ていないかもしれませんが、それは完全に病理学的な使用のように思えます(そして、それでも本当にそれが必要な場合は、 でアクセスできますobject.__new__.__func__
)。少なくとも、それが Guido__new__
のクラス メソッドから静的メソッドへの変更の理由だったとは想像しがたいと思います。
より一般的なケースは、__new__
を使用せずに親を呼び出すことsuper()
です。この場合、明示的に渡す場所が必要ですcls
:
class Base(object):
@classmethod
def new(cls):
print("Base.new(%r)" % (cls,))
return cls()
class UseSuper(Base):
@classmethod
def new(cls):
print("UseSuper.new(%r)" % (cls,))
return super(UseSuper, cls).new() # passes cls as the first arg
class NoSuper(Base):
@classmethod
def new(cls):
print("NoSuper.new(%r)" % (cls,))
return Base.new() # passes Base as the first arg
class UseFunc(Base):
@classmethod
def new(cls):
print("UseFunc.new(%r)" % (cls,))
return Base.new.im_func(cls) # or `.__func__(cls)`. # passes cls as the first arg
print(UseSuper.new())
print('-'*60)
print(NoSuper.new())
print('-'*60)
print(UseFunc.new())