I would like to clarify some things about Python's descriptors. I want to add a property to my class with some complex set/get mechanics and cache those calculated values inside the descriptor object. A simplified example looks like this:
class Pro(object):
"""My descriptor class"""
def __init__(self):
self.value = None
def __get__(self, instance, owner):
return self.value
def __set__(self, instance, value):
self.value = value
class Something(object):
"""My simple class"""
pro = Pro()
a = Something()
a.pro = 1
b = Something()
b.pro = 2
print(a.pro, b.pro) # At first, I've expected them to be 1 and 2
I thought somehow that pro attribute would be unique instance of Pro for every instance of Something, obviously I was wrong. Looks like I should use something like instance._value instead of self.value inside __set__ and __get__, but I've really hoped to hide everything inside Pro class. Is this even possible? Thanks!