2
# -*- coding: utf-8 -*-
5
class cachedproperty(object):
9
... def __init__(self, x):
15
... def set_x(self, newx):
16
... print "setting x with %s" % newx
19
... self._x = "i am deleted"
21
... x = cachedproperty(get_x, set_x, del_x)
22
... other_x = cachedproperty(get_x, set_x)
48
# ok with multiple instances.
70
# but the property cant konw about it...
77
__slots__ = ("fget", "fset", "fdel", "n")
79
def __init__(self, fget=None, fset=None, fdel=None):
83
self.n = "__" + fget.__name__
85
def __get__(self, o, otype=None):
88
if self.n in o.__dict__:
89
return (o.__dict__)[self.n]
90
result = (o.__dict__)[self.n] = self.fget(o)
93
def __set__(self, o, value):
95
raise AttributeError, "unsettable %s (with %s)" % (self.n,
99
print self.n, o, value
100
if self.n in o.__dict__:
101
del (o.__dict__)[self.n]
104
def __delete__(self, o):
105
if self.fdel is None:
106
raise AttributeError, "undeletable %s (with %s)" % (self.n,
109
if self.n in o.__dict__:
110
del (o.__dict__)[self.n]
114
if __name__ == "__main__":