Implement lazy_factory 🍰

This commit is contained in:
Kristóf Tóth 2018-10-09 11:25:11 +02:00
parent 031400c0c4
commit 1d969f0967
1 changed files with 10 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from functools import update_wrapper
from functools import update_wrapper, wraps
class lazy_property:
@ -19,3 +19,12 @@ class lazy_property:
value = self.func(instance)
setattr(instance, self.func.__name__, value)
return value
def lazy_factory(fun):
class wrapper:
@wraps(fun)
@lazy_property
def instance(self): # pylint: disable=no-self-use
return fun()
return wrapper()