forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreify.py
More file actions
46 lines (35 loc) · 1.13 KB
/
reify.py
File metadata and controls
46 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Originally taken from pyramid.decorator
class reify(object):
"""Cache a property.
Use as a method decorator. It operates almost exactly like the
Python ``@property`` decorator, but it puts the result of the
method it decorates into the instance dict after the first call,
effectively replacing the function it decorates with an instance
variable. It is, in Python parlance, a non-data descriptor. An
example:
.. testcode::
from morepath import reify
class Foo(object):
@reify
def jammy(self):
print('jammy called')
return 1
And usage of Foo:
>>> f = Foo()
>>> v = f.jammy
jammy called
>>> print(v)
1
>>> print(f.jammy)
1
>>> # jammy func not called the second time; it replaced itself with 1
"""
def __init__(self, wrapped):
self.wrapped = wrapped
self.__doc__ = wrapped.__doc__
def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val