pyramid.decorator

reify(wrapped)[源代码]

用作类方法修饰器。它的运行方式几乎与Python完全相同 @property decorator,但它在第一次调用后将它所修饰的方法的结果放入实例dict,有效地用实例变量替换它所修饰的函数。用Python的话说,它是一个非数据描述符。以下是一个示例及其用法:

>>> from pyramid.decorator import reify

>>> class Foo(object):
...     @reify
...     def jammy(self):
...         print('jammy called')
...         return 1

>>> f = Foo()
>>> v = f.jammy
jammy called
>>> print(v)
1
>>> f.jammy
1
>>> # jammy func not called the second time; it replaced itself with 1
>>> # Note: reassignment is possible
>>> f.jammy = 2
>>> f.jammy
2