简体   繁体   English

在类中设置python方法的属性

[英]Setting Attribute Of python Method In Class

I can't set an attribute of a function inside of a class. 我无法在类内部设置函数的属性。 Minimal example: 最小示例:

class Foo:
    def bar(self):
        pass

f = Foo()
f.bar.attribute = True 

For me, this throws an AttributeError: 'method' object has no attribute 'attribute' ( setattr(f.bar, 'attribute', True) instead of the last line throws the same error). 对我来说,这会引发AttributeError: 'method' object has no attribute 'attribute'setattr(f.bar, 'attribute', True)而不是最后一行会引发相同的错误)。

Is there a more elegant or pythonic way to set the attribute than f.bar.__dict__['attribute'] = True ? 是否有比f.bar.__dict__['attribute'] = True方式设置属性?

Method objects don't have anywhere to store their own arbitrary attributes. 方法对象没有任何地方可以存储自己的任意属性。 Also, method objects are created on each access - f.bar is not f.bar - so even if you could set an attribute on a method object, it wouldn't be there when you tried to look for it again. 同样,在每次访问时f.bar is not f.bar创建方法对象f.bar is not f.bar因此,即使您可以在方法对象上设置属性,当您尝试再次查找它时,该属性也不会存在。 This means that you really shouldn't try to set attributes on method objects. 这意味着您实际上不应该尝试在方法对象上设置属性。

When you ask for an attribute a method object doesn't recognize, it forwards the request to the underlying function, so f.bar.__dict__ is really Foo.bar.__dict__ . 当您请求方法对象无法识别的属性时,它会将请求转发到基础函数,因此f.bar.__dict__实际上是Foo.bar.__dict__ Setting entries in that dict will set attributes on the underlying function, so they'll be visible through every instance of Foo , and methods of separate instances won't have independent attributes. 在该字典中设置条目将设置基础函数的属性,因此它们将在Foo每个实例中可见,并且单独实例的方法将没有独立的属性。

Whatever problem you were trying to solve by setting attributes on method objects, you should find some other way to solve it. 无论您试图通过在方法对象上设置属性来解决什么问题,都应该找到其他解决方法。

I can't set an attribute of a function inside of a class. 我无法在类内部设置函数的属性。

Actually, you can. 其实可以。 What you tried to do was set an attribute of the particular method off of an instance of the class. 您试图做的是从类的实例设置特定方法的属性。 That, you can't do (AFAIK). 那是你做不到的(AFAIK)。

Foo is the class. Foo是班。 Try Foo.bar.attribute = True . 尝试Foo.bar.attribute = True This will affect all instances of Foo , not just f . 这将影响Foo 所有实例,而不仅是f For example: 例如:

class Foo:
    def bar(self):
        pass

f = Foo()
Foo.bar.attribute = True
print(f.bar.attribute) # outputs True
z = Foo()
print(z.bar.attribute) # Also outputs True

In fact, f.bar.__dict__['attribute'] = True actually sets it at the class level as well: 实际上, f.bar.__dict__['attribute'] = True实际上也在类级别设置了它:

class Foo:
    def bar(self):
        pass

f = Foo()
f.bar.__dict__['attribute'] = True
print(f.bar.attribute) # Outputs True                                                                                                                                                                       
z = Foo()
print(z.bar.attribute) # Also Outputs True 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM