__new__ 对实例化感到困惑
__new__ confusion regarding instantiation
我正在努力了解 __new__
的工作原理,但目前我只是想不通。
根据 this blog post explaining the __new__
method,为了构建我的实例,我这样做了:super(MyClass, cls).__new__(cls, *args, **kwargs)
然而,每当我指示我的 __new__
从另一个 class 实例化时,它似乎没有接受参数,或者如果我这样做 Bar.__new__(cls)
它似乎没有完全接受这一点。彻底糊涂了。
我的代码:
class Bar(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
def hello(self):
print("HelloBar")
class Foo(object):
def __new__(cls, *args, **kwargs):
return Bar.__new__(cls) # Have tried doing stuff like
# super().__new__(bar) as well, no success
def __init__(self, a, b):
self.a = a
self.b = b
i = Foo(2,3,4)
我正在尝试指向 class Bar
的实例,我该怎么做?
直接调用Bar()
即可; Bar()
完全是一个不同的 class,Foo.__new__
不必处理 class 的实现方式。
你 do 必须传递你的 Foo(...)
调用被传递的参数,如果你想能够创建一个 Bar()
实例:
class Foo(object):
def __new__(cls, *args, **kwargs):
return Bar(*args, **kwargs)
您只需要在 Foo.__new__
中使用 super().__new__()
,因为您已经覆盖了 object.__new__
方法,但仍然需要访问它来创建实际实例。
您仍然可以调用 Bar.__new__(Bar, *args, **kwargs)
或 object.__new__(Bar, *args, **kwargs)
,但您将绕过 Bar.__init__
方法。那是因为 ClassObject(...)
被翻译成 type(ClassObject).__call__(ClassObject, ...)
,然后 __call__()
方法的工作是调用 __new__
,然后仅当 [=26] 时才调用 __init__
=] 产生了一个 ClassObject
的实例。用Python代码表示,有点像这样:
def __call__(type_, cls, *args, **kwargs):
instance = cls.__new__(cls, *args, **kwargs)
if isinstance(instance, cls):
instance.__init__(*args, **kwargs)
return instance
如果 Foo.__new__()
returns 是 Bar
的实例,则 isinstance(instace, Foo)
为假,type(Foo).__call__()
实现将不会调用 __init__
该对象上的方法。通过调用 Bar()
,您可以确保 type(Bar).__call__()
已完成调用 Bar.__init__()
.
的工作
我正在努力了解 __new__
的工作原理,但目前我只是想不通。
根据 this blog post explaining the __new__
method,为了构建我的实例,我这样做了:super(MyClass, cls).__new__(cls, *args, **kwargs)
然而,每当我指示我的 __new__
从另一个 class 实例化时,它似乎没有接受参数,或者如果我这样做 Bar.__new__(cls)
它似乎没有完全接受这一点。彻底糊涂了。
我的代码:
class Bar(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
def hello(self):
print("HelloBar")
class Foo(object):
def __new__(cls, *args, **kwargs):
return Bar.__new__(cls) # Have tried doing stuff like
# super().__new__(bar) as well, no success
def __init__(self, a, b):
self.a = a
self.b = b
i = Foo(2,3,4)
我正在尝试指向 class Bar
的实例,我该怎么做?
直接调用Bar()
即可; Bar()
完全是一个不同的 class,Foo.__new__
不必处理 class 的实现方式。
你 do 必须传递你的 Foo(...)
调用被传递的参数,如果你想能够创建一个 Bar()
实例:
class Foo(object):
def __new__(cls, *args, **kwargs):
return Bar(*args, **kwargs)
您只需要在 Foo.__new__
中使用 super().__new__()
,因为您已经覆盖了 object.__new__
方法,但仍然需要访问它来创建实际实例。
您仍然可以调用 Bar.__new__(Bar, *args, **kwargs)
或 object.__new__(Bar, *args, **kwargs)
,但您将绕过 Bar.__init__
方法。那是因为 ClassObject(...)
被翻译成 type(ClassObject).__call__(ClassObject, ...)
,然后 __call__()
方法的工作是调用 __new__
,然后仅当 [=26] 时才调用 __init__
=] 产生了一个 ClassObject
的实例。用Python代码表示,有点像这样:
def __call__(type_, cls, *args, **kwargs):
instance = cls.__new__(cls, *args, **kwargs)
if isinstance(instance, cls):
instance.__init__(*args, **kwargs)
return instance
如果 Foo.__new__()
returns 是 Bar
的实例,则 isinstance(instace, Foo)
为假,type(Foo).__call__()
实现将不会调用 __init__
该对象上的方法。通过调用 Bar()
,您可以确保 type(Bar).__call__()
已完成调用 Bar.__init__()
.