简体   繁体   English

如何在描述符外部访问存储在python描述符对象中的状态?

[英]How can state stored in a python descriptor object be accessed outside the descriptor?

Is it possible to get at state stored in a python descriptor? 是否有可能获得存储在python描述符中的状态?

For example: 例如:

class Price(object):
    def __init__(self, dollars):
        self.dollars = dollars

    class Convert(object):
        def __init__(self, rate):
            self.rate = rate
        def __get__(self, instance, owner):
            return instance.dollars * self.rate
    euros = Convert(0.75)

p = Price(20.0)

print p.dollars
print p.euros

works as expected; 按预期工作; however, I'd like to get at the rate that is stored within the Convert descriptor that is managing p.euros is that possible? 但是,我想得到Convert描述符中存储的rate ,管理p.euros是可能的吗? Clearly p.euros.rate won't work but I'm not sure how to get at the euros instance of Converter. 显然p.euros.rate不起作用,但我不知道如何获得转换器的欧元实例。

(I realize that in this simplified case making euros a property and putting rate on price makes sense it was just a simple example) (我意识到在这个简化的案例中,将欧元作为财产并将价格置于价格上是有道理的,这只是一个简单的例子)

Actually @yak's comment on his own answer above gives the right way to do it: using the class __dict__ attribute. 实际上@yak对自己上面回答的评论给出了正确的方法:使用类__dict__属性。

Therefore, you can get the descriptor by doing, (on your code, which lists p as an instance of Price ) by doing: p.__class__.__dict__["euros"] 因此,您可以通过执行以下操作来获取描述符(在您的代码上,列出p作为Price的实例): p.__class__.__dict__["euros"]

Note however that the descriptor itself is a "class variable" - so, still on your example, if you need different "Price" objects to have different "rate" attributes on the "euro" attribute, the Convert descriptor has to keep the "rate" not as self.rate, but in some (possibly name mangled) attribute of the "instance" parameter passed to the "__get__" and "__set__" methods. 但请注意,描述符本身是一个“类变量” - 所以,仍然在你的例子中,如果你需要不同的“Price”对象在“euro”属性上有不同的“rate”属性,转换描述符必须保持“ rate“不是self.rate,而是传递给”__get__“和”__set__“方法的”instance“参数的某些(可能是名称损坏的)属性。

If that is the case, you could access that attribute directly on the instance itself - or implement some "get" and "setters" (or a nested property) for dealing with the descirptor attribute you want to manipulate in the descriptor's class itself. 如果是这种情况,您可以直接在实例本身上访问该属性 - 或者实现一些“get”和“setters”(或嵌套属性)来处理您想要在描述符类本身中操作的descirptor属性。

If you access the descriptor through the class, the instance argument of __get__ will be None. 如果通过类访问描述符,则__get__instance参数将为None。 You can check for that and return the descriptor itself: 您可以检查并返回描述符本身:

class Price(object):
    def __init__(self, dollars):
        self.dollars = dollars

    class Convert(object):
        def __init__(self, rate):
            self.rate = rate

        def __get__(self, instance, owner):
            if instance is None:
                # Accessed through class, return the descriptor instead.
                return self
            return instance.dollars * self.rate

    euros = Convert(0.75)

Then you get the rate like this: 然后你得到这样的速度:

>>> Price.euros.rate
0.75

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

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