Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Static caching property Date: Tue, 22 Mar 2016 09:30:03 +0100 Lines: 22 Message-ID: References: <35a5c4206a0c40e584d62d5d37b068b3@activenetwerx.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de pTj947olN4UPYVXFIoBaNQ8SywmJEi86E3uLdyQgGNJg== Cancel-Lock: sha1:B8o3yFcCN3wbF9iF5MLtvbUpgg0= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cache': 0.05; 'none:': 0.05; 'defines': 0.07; 'instances.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; '@classmethod': 0.16; 'email name:"': 0.16; 'instances,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'attribute': 0.18; 'this:': 0.23; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'class': 0.33; 'instance': 0.35; 'something': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'determine': 0.61; 'results': 0.66 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57b39509.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105454 "Joseph L. Casale" 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".