object.__new__, Exception.__new__.
Exception , , __new__ , , .
, . Python , .
:
class A(object):
def __new__(cls):
rtn = object.__new__(cls)
rtn.new_called = True
return rtn
def __init__(self):
assert getattr(self,'new_called',False), \
"object.__new__ is unsafe, use A.__new__"
class B(A):
def __new__(cls):
return object.__new__(cls)
:
>>> A()
<__main__.A object at 0x00000000025CFF98>
>>> B()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __init__
AssertionError: object.__new__ is unsafe, use A.__new__
:
>>> class A(Exception):
... def __new__(cls, *args):
... return object.__new__(A)
-, __new__ object, Exception.__new__.
, , A __new__ cls, , A.
. :
class A(object):
def __new__(cls):
return object.__new__(A)
class B(A):
pass
B() B:
>>> B()
<__main__.A object at 0x00000000025D30B8>