Python:如何理解内置对象class中定义的__new__?
Python: How to understand __new__ as defined in the built-in object class?
首先,这不是 Why is __init__ not called after __new__ SOMETIMES 的重复,因为这个问题实际上是关于内置 object
class 的实现。
这是完整的故事:
我正在学习 __new__
与 python 中的 __init__
。这是我试过的例子:
class A(object):
def __new__(cls):
print("A.__new__ called")
return super(A, cls).__new__(cls)
def __init__(self):
print("A.__init__ called")
A()
print('finished')
输出是
A.__new__ called
A.__init__ called
finished
我了解 __new__
进行对象创建而 __init__
进行对象初始化。
__new__
在调用 class 名称时自动调用。然后,每次 class 的实例被 __new__
编辑时,都会调用 __init__
,将 return 编辑的实例作为 __init__
传递给 __init__
self
参数。
知道这一点,如果我们有一个不创建的错误 __new__
函数和 return 一个对象实例,那么 __init__
将不会被调用:
class A(object):
def __new__(cls):
print("A.__new__ called")
#return super(A, cls).__new__(cls)
def __init__(self):
print("A.__init__ called")
A()
请注意,__new__
方法仅打印一个字符串。它没有 return 任何东西,所以 __init__
没有什么可拾取的。事实上,输出证实了这一点:
A.__new__ called
finished
"A.__init__ called"
从未打印过,因此 __init__
确实从未被调用过。
现在如果我们不定义一个__new__
方法(这是99%的常见用例,程序员很少需要定义这个方法),那么默认调用爸爸的__new__
.例如
class A(object):
def __init__(self):
print("A.__init__ called")
A()
print('finished')
输出为:
A.__init__ called
finished
在这种情况下,内置 object
class 的 __new__
被调用。
但是,当我查看内置 object
class 的定义方式时,我看到了:
class object:
""" The most base type """
def __delattr__(self, *args, **kwargs): # real signature unknown
""" Implement delattr(self, name). """
pass
...
...
...
@staticmethod # known case of __new__
def __new__(cls, *more): # known special case of object.__new__
""" Create and return a new object. See help(type) for accurate signature. """
pass
__new__
中没有实现!里面只有一个 pass
!!??
这有什么意义?
一些 CPython 实现是用 C 语言编写的,因此 PyCharm 无法导航到源代码,因为它是经过编译的。但是它可以并且确实显示模拟签名的存根。
3.7.3 的 object.__new__
源代码可以在他们的 Github 存储库中找到:https://github.com/python/cpython/blob/v3.7.3/Objects/typeobject.c#L3671-L3732
首先,这不是 Why is __init__ not called after __new__ SOMETIMES 的重复,因为这个问题实际上是关于内置 object
class 的实现。
这是完整的故事:
我正在学习 __new__
与 python 中的 __init__
。这是我试过的例子:
class A(object):
def __new__(cls):
print("A.__new__ called")
return super(A, cls).__new__(cls)
def __init__(self):
print("A.__init__ called")
A()
print('finished')
输出是
A.__new__ called
A.__init__ called
finished
我了解 __new__
进行对象创建而 __init__
进行对象初始化。
__new__
在调用 class 名称时自动调用。然后,每次 class 的实例被 __new__
编辑时,都会调用 __init__
,将 return 编辑的实例作为 __init__
传递给 __init__
self
参数。
知道这一点,如果我们有一个不创建的错误 __new__
函数和 return 一个对象实例,那么 __init__
将不会被调用:
class A(object):
def __new__(cls):
print("A.__new__ called")
#return super(A, cls).__new__(cls)
def __init__(self):
print("A.__init__ called")
A()
请注意,__new__
方法仅打印一个字符串。它没有 return 任何东西,所以 __init__
没有什么可拾取的。事实上,输出证实了这一点:
A.__new__ called
finished
"A.__init__ called"
从未打印过,因此 __init__
确实从未被调用过。
现在如果我们不定义一个__new__
方法(这是99%的常见用例,程序员很少需要定义这个方法),那么默认调用爸爸的__new__
.例如
class A(object):
def __init__(self):
print("A.__init__ called")
A()
print('finished')
输出为:
A.__init__ called
finished
在这种情况下,内置 object
class 的 __new__
被调用。
但是,当我查看内置 object
class 的定义方式时,我看到了:
class object:
""" The most base type """
def __delattr__(self, *args, **kwargs): # real signature unknown
""" Implement delattr(self, name). """
pass
...
...
...
@staticmethod # known case of __new__
def __new__(cls, *more): # known special case of object.__new__
""" Create and return a new object. See help(type) for accurate signature. """
pass
__new__
中没有实现!里面只有一个 pass
!!??
这有什么意义?
一些 CPython 实现是用 C 语言编写的,因此 PyCharm 无法导航到源代码,因为它是经过编译的。但是它可以并且确实显示模拟签名的存根。
3.7.3 的 object.__new__
源代码可以在他们的 Github 存储库中找到:https://github.com/python/cpython/blob/v3.7.3/Objects/typeobject.c#L3671-L3732