I was looking at some lazy loading property decorators in Python and happened across 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 this work? My understanding of decorators is that they must be callable (so either a function or a call that implements __call__), but Lazy here clearly is not and if I try Lazy(someFunc)() it raises an exception as expected. What am I missing?
When an attribute named
somepropis accessed on instanceoof classSomeClass, ifSomeClasscontains a descriptor namedo, then that descriptor’s class’s__get__method is used. For more on descriptors, see this guide. Don’t let the fact thatLazyis here used, syntactically, as a decorator, blind you to the fact that its instances are descriptors, becauseLazyitself has a__get__method.The decorator syntax
is no more, and no less, than syntax sugar for:
The constraints on
Lazyare no different when it’s used with decorator syntax or directly: it must acceptsomeprop(a function) as its argument — no constraints whatsoever on what it returns. Here,Lazyis a class so it returns an instance of itself, and has a__get__special method so that instance is a descriptor (so said method gets called when thesomepropattribute is accessed on the instanceoof classSomeClass) — that’s all there is to it, no more, and no less.