问题描述
在以下情况中,setattr
在第一次调用中成功,但在第二次调用中失败:
In the following, setattr
succeeds in the first invocation, but fails in the second, with:
AttributeError: 'method' object has no attribute 'i'
这是为什么,是否有一种方法可以在方法上设置属性,使其仅存在于一个实例上,而不是针对类的每个实例?
Why is this, and is there a way of setting an attribute on a method such that it will only exist on one instance, not for each instance of the class?
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
推荐答案
简短的答案:无法将自定义属性添加到绑定方法中.
The short answer: There is no way of adding custom attributes to bound methods.
长答案如下.
在Python中,有个功能对象和一种方法对象.定义类时,def
语句创建一个 function对象,该对象位于该类的名称空间中:
In Python, there are function objects and method objects. When you define a class, the def
statement creates a function object that lives within the class' namespace:
>>> class c:
... def m(self):
... pass
...
>>> c.m
<function m at 0x025FAE88>
函数对象具有特殊的__dict__
属性,该属性可以保存用户定义的属性:
Function objects have a special __dict__
attribute that can hold user-defined attributes:
>>> c.m.i = 0
>>> c.m.__dict__
{'i': 0}
方法对象是不同的野兽.它们是微小的对象,仅持有对相应功能对象(__func__
)的引用,并对其宿主对象(__self__
)的引用:
Method objects are different beasts. They are tiny objects just holding a reference to the corresponding function object (__func__
) and one to its host object (__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__
,该属性将属性访问转发给功能对象:
Method objects provide a special __getattr__
that forwards attribute access to the function object:
>>> c().m.i
0
对于__dict__
属性也是如此:
>>> c().m.__dict__['a'] = 42
>>> c.m.a
42
>>> c().m.__dict__ is c.m.__dict__
True
但是,设置属性遵循默认规则,并且由于它们没有自己的__dict__
,因此无法设置任意属性.
Setting attributes follows the default rules, though, and since they don't have their own __dict__
, there is no way to set arbitrary attributes.
这类似于定义__slots__
并且没有__dict__
插槽的用户定义类,当尝试设置不存在的插槽时会引发AttributeError
(请参见上的文档以获取更多信息):
This is similar to user-defined classes defining __slots__
and no __dict__
slot, when trying to set a non-existing slot raises an AttributeError
(see the docs on __slots__
for more information):
>>> 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'
这篇关于为什么setattr在绑定方法上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!