[Python-ideas] caching properties

Jeremy Banks jeremy at jeremybanks.ca
Thu Apr 30 14:36:17 CEST 2009


I think one thing that should be addressed is how to change the cached
value, for example after the setter is called. Having multiple
decorators would seem to make this slightly more complicated. If we
were using only one, it would be easy to just add a cache property to
the decorated function, which the setter could easily deal with.

  @property(cache=True)
  def foo(self):
    return(self.bar * 99)

  @foo.setter
  def foo(self, bar):
    self.bar = bar / 99
    del foo.cache # or foo.cache = bar

I guess this doesn't actually work anyway because of the way setters
are specified, since it would refer to the setter instead of the
property. Hmm. Perhaps, since the return value of setters is currently
meaingless, it could be written into the cache, or used to specify if
the cache should be cleared? Or just clear the cache automatically any
time the setter is called.

I realize that these ideas would probably introduce excessive overhead
and have some other problems, but I think this is something that needs
to be figured out before too long, so having some more ideas out there
probably won't hurt.

- JB

On 2009-04-30, spir <denis.spir at free.fr> wrote:
> Le Thu, 30 Apr 2009 13:57:07 +1000,
> Steven D'Aprano <steve at pearwood.info> s'exprima ainsi:
>
>> -1 on an extra parameter to property.
>>
>> +1 on a cache decorator.
>>
>> It is clear and simple enough to write something like:
>>
>> @property
>> @cache  # or "once" if you prefer the Eiffel name
>> def value(self):
>>     pass
>
> I vote +1 for this version.
> Cache and property are distinct features.
>
> Denis
> ------
> la vita e estrany
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list