【发布时间】:2018-02-15 01:03:33
【问题描述】:
在 Python 2 中:
>>> class A:
... pass
...
>>> A.__new__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: class A has no attribute '__new__'
>>> class A(object):
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x1062fe2a0>
结论:object包含__new__,子类继承该方法。
在 Python 3 中:
>>> class A:
... pass
...
>>> A.__new__
<built-in method __new__ of type object at 0x100229940>
__new__ 是我们类中定义的方法,没有任何继承。这是如何运作的? __new__“从何而来”?
【问题讨论】:
-
查看
A.__mro__...
标签: python