本篇文章小编给大家分享一下Python中@lazyprop装饰器用法代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
安装
pip install lazyprop
例子1
from lazyprop import lazyprop class Foo(object): def __init__(self): self.load_count = 0 @lazyprop def lazy(self): self.load_count += 1 f = Foo() f.lazy f.lazy f.lazy print(f.load_count)
输出:
1
例子2
from lazyprop import lazyprop class Foo(object): def __init__(self): self.load_count = 0 # @lazyprop def lazy(self): self.load_count += 1 f = Foo() f.lazy f.lazy f.lazy print(f.load_count)
输出:
0