Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pyramid/decorator.py : 91%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from functools import update_wrapper
4class reify(object):
5 """ Use as a class method decorator. It operates almost exactly like the
6 Python ``@property`` decorator, but it puts the result of the method it
7 decorates into the instance dict after the first call, effectively
8 replacing the function it decorates with an instance variable. It is, in
9 Python parlance, a non-data descriptor. The following is an example and
10 its usage:
12 .. doctest::
14 >>> from pyramid.decorator import reify
16 >>> class Foo(object):
17 ... @reify
18 ... def jammy(self):
19 ... print('jammy called')
20 ... return 1
22 >>> f = Foo()
23 >>> v = f.jammy
24 jammy called
25 >>> print(v)
26 1
27 >>> f.jammy
28 1
29 >>> # jammy func not called the second time; it replaced itself with 1
30 >>> # Note: reassignment is possible
31 >>> f.jammy = 2
32 >>> f.jammy
33 2
34 """
36 def __init__(self, wrapped):
37 self.wrapped = wrapped
38 update_wrapper(self, wrapped)
40 def __get__(self, inst, objtype=None):
41 if inst is None:
42 return self
43 val = self.wrapped(inst)
44 setattr(inst, self.wrapped.__name__, val)
45 return val