Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105454 > unrolled thread

Re: Static caching property

Started bydieter <dieter@handshake.de>
First post2016-03-22 09:30 +0100
Last post2016-03-22 09:30 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Static caching property dieter <dieter@handshake.de> - 2016-03-22 09:30 +0100

#105454 — Re: Static caching property

Fromdieter <dieter@handshake.de>
Date2016-03-22 09:30 +0100
SubjectRe: Static caching property
Message-ID<mailman.487.1458635413.12893.python-list@python.org>
"Joseph L. Casale" <jcasale@activenetwerx.com> writes:
> ...
> I need to cache the results of a method on a class across all instances.

If a method call on any instance defines the return value for
all instances, then this method likely should be a class method --
and use a class attribute to store the result -- something like this:

    class C(object):

    _cache = {}

    @classmethod
    def f(cls, ...):
      ... determine cache key `key` ...
      v = cls._cache.get(key)
      if v is None:
         v = cls._cache[key] = ...
      return v

It will work also without the "@classmethod".

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web