[Python-ideas] caching properties
spir
denis.spir at free.fr
Thu Apr 30 16:53:09 CEST 2009
Le Thu, 30 Apr 2009 05:55:41 -0700,
Daniel Fetchinson <fetchinson at googlemail.com> s'exprima ainsi:
> > It is clear and simple enough to write something like:
> >
> > @property
> > @cache # or "once" if you prefer the Eiffel name
> > def value(self):
> > pass
> >
> > Such a decorator could then be used on any appropriate function, not
> > just for properties.
>
> I agree, -1 on modifying @property and +1 on adding a new decorator to
> functools or some other module, maybe a collection of useful
> decorators.
While I regularly use caching/memoizing (e.g. for packrat parsing) and I like the idea of having it predefined in the language, now I wonder whether it's really worth it. Because usually it's a very simple thing to implement -- often two obvious lines of code -- and easy to understand, even for someone who does not know the principle yet.
E.g for a property getter:
def _getThing(...):
if self._thing is not None:
return self._thing
<else compute thing>
(Also note that for a func you can cache on the func itself: func.cache -- but the 2-3 cases when I thought at that, it was rather a design issue with global vars.)
Maybe I'm missing the point?
Denis
------
la vita e estrany
More information about the Python-ideas
mailing list