在下文中,setattr
在第一次调用中成功,但在第二次调用中失败,如下:
AttributeError: 'method' object has no attribute 'i'
为什么会这样,有没有办法在方法上设置一个属性,使其只存在于一个实例上,而不是类的每个实例上?
class c:
def m(self):
print(type(c.m))
setattr(c.m, 'i', 0)
print(type(self.m))
setattr(self.m, 'i', 0)
Python 3.2.2
最佳答案
简短的回答:无法将自定义属性添加到绑定(bind)方法。
下面是长答案。
在Python中,有函数对象和方法对象。当您定义一个类时,def
语句会创建一个 函数对象,该对象位于类的命名空间中:
>>> class c:
... def m(self):
... pass
...
>>> c.m
<function m at 0x025FAE88>
函数对象有一个特殊的 __dict__
属性,可以保存用户定义的属性:
>>> c.m.i = 0
>>> c.m.__dict__
{'i': 0}
方法对象是不同的野兽。它们是微小的对象,仅包含对相应函数对象 (__func__
) 的引用和对其宿主对象 (__self__
) 的引用:
>>> c().m
<bound method c.m of <__main__.c object at 0x025206D0>>
>>> c().m.__self__
<__main__.c object at 0x02625070>
>>> c().m.__func__
<function m at 0x025FAE88>
>>> c().m.__func__ is c.m
True
方法对象提供了一个特殊的__getattr__
,它将属性访问转发给函数对象:
>>> c().m.i
0
__dict__
属性也是如此:
>>> c().m.__dict__['a'] = 42
>>> c.m.a
42
>>> c().m.__dict__ is c.m.__dict__
True
但是,设置属性遵循默认规则,并且由于它们没有自己的 __dict__
,因此无法设置任意属性。
这类似于定义 __slots__
且没有 __dict__
插槽的用户定义类,当尝试设置不存在的插槽时会引发 AttributeError
(参见 docs on __slots__
了解更多信息):
>>> class c:
... __slots__ = ('a', 'b')
...
>>> x = c()
>>> x.a = 1
>>> x.b = 2
>>> x.c = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'c' object has no attribute 'c'
关于python - 为什么 setattr 在绑定(bind)方法上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7891277/