]>
Commit | Line | Data |
---|---|---|
de9dc716 MT |
1 | #!/usr/bin/python3 |
2 | ||
3 | class Missing(object): | |
4 | pass | |
5 | ||
6 | _missing = Missing() | |
7 | ||
8 | class lazy_property(property): | |
9 | """ | |
10 | The property is only computed once and then being | |
11 | cached until the end of the lifetime of the object. | |
12 | """ | |
13 | def __init__(self, fget, fset=None, fdel=None, doc=None, name=None): | |
14 | property.__init__(self, fget=fget, fset=fset, fdel=fdel, doc=doc) | |
15 | ||
16 | self.__name__ = name or fget.__name__ | |
17 | self.__module__ = fget.__module__ | |
18 | ||
19 | def __get__(self, obj, type=None): | |
20 | if object is None: | |
21 | return self | |
22 | ||
23 | value = obj.__dict__.get(self.__name__, _missing) | |
24 | if value is _missing: | |
25 | obj.__dict__[self.__name__] = value = self.fget(obj) | |
26 | ||
27 | return value | |
28 | ||
29 | def __set__(self, obj, value): | |
30 | if self.fset: | |
31 | self.fset(obj, value) | |
32 | ||
33 | obj.__dict__[self.__name__] = value |