新式类属性__call__
>> class A(object):
... def __call__(self):... print("invoking __call__ from A!")
...
...
...
>>> a = A()
invoking __call__ from A!
a实例字典__call__
>>> a = A()>>> a.__call__ = lambda: "invoking __call__ from lambda"
>>> a.__call__()
'invoking __call__ from lambda'
实例字典和新式类属性是相互隔离的
新式类属性对应type(X).__dict__
>>> a()invoking __call__ from A!
>>> class A(object):
... def __call__(self):
... print("invoking __call__ from A!")
... def __add__(self):
... return self
...
...
>>> d = A()
>>> type(d).__dict__
dict_proxy({'__module__': '__console__', '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__dict__': <attribute '__dict__' of 'A' objects>, '__add__': <function __add__ at 0x2830938>, '__call__': <function __call__ at 0x2830668>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
实例字典对应X.__dict__
>>> d.__add__ = 1
>>> d.__dict__
{'__add__': 1}
dict_proxy({'__module__': '__console__', '__getattribute__': <slot wrapper '__getattribute__' of 'object' objects>, '__dict__': <attribute '__dict__' of 'A' objects>, '__add__': <function __add__ at 0x2830938>, '__call__': <function __call__ at 0x2830668>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None})
来自:https://buluo.qq.com/p/detail.html?bid=21580&pid=4005775-1483516970&from=share_qq