I looked at some lazy decorators of loading properties in Python and happened in this example ( http://code.activestate.com/recipes/363602-lazy-property-evaluation/ ):
class Lazy(object): def __init__(self, calculate_function): self._calculate = calculate_function def __get__(self, obj, _=None): if obj is None: return self value = self._calculate(obj) setattr(obj, self._calculate.func_name, value) return value # Sample use: class SomeClass(object): @Lazy def someprop(self): print 'Actually calculating value' return 13 o = SomeClass() o.someprop o.someprop
My question is: how does it work? My understanding of decorators is that they must be callable (so either a function or a call that implements __call__), but Lazythis is clearly not there, and if I try Lazy(someFunc)(), it throws an exception as expected. What am I missing?
__call__
Lazy
Lazy(someFunc)()
someprop o SomeClass, SomeClass o, __get__. . . , Lazy , , , , , Lazy __get__.
someprop
o
SomeClass
__get__
@Lazy def someprop(self): ...
, :
def someprop(self): ... someprop = Lazy(someprop)
Lazy , : someprop () - , . Lazy - , __get__, ( , o someprop SomeClass) - , , .
Source: https://habr.com/ru/post/1751419/More articles:Can't pass "% 26" to the WebGet UriTemplate variable in the WCF service? - iisИспользование Invoke с элементом пользовательского интерфейса, который расширяет контекст приложения? - multithreadingrails: exchange of information between 2 applications - databaseRemove cookie from XMLHttpRequest object - javascriptjQuery: determine that the DOM has changed - javascriptHow are package names defined in java, why is "com" the root? - flashQueue with cflock on Coldfusion 9 - javaY-Combinator in FT EDSL - haskellКогда следует использовать $(document).ready? - javascriptIs there a performance difference between the two instructions? - javaAll Articles