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


Groups > comp.lang.python > #105454

Re: Static caching property

From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Static caching property
Date 2016-03-22 09:30 +0100
Message-ID <mailman.487.1458635413.12893.python-list@python.org> (permalink)
References <35a5c4206a0c40e584d62d5d37b068b3@activenetwerx.com>

Show all headers | View raw


"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".

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web