morepath.reify – Caching property

class morepath.reify.reify(wrapped)

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:

from morepath import reify

class Foo(object):
    def jammy(self):
        print('jammy called')
        return 1

And usage of Foo:

>>> f = Foo()
>>> v = f.jammy
jammy called
>>> print(v)
>>> print(f.jammy)
>>> # jammy func not called the second time; it replaced itself with 1