Is it possible to get at state stored in a python descriptor?
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? Clearly p.euros.rate won’t work but I’m not sure how to get at the euros instance of Converter.
(I realize that in this simplified case making euros a property and putting rate on price makes sense it was just a simple example)
If you access the descriptor through the class, the
instanceargument of__get__will be None. You can check for that and return the descriptor itself:Then you get the rate like this: