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


Groups > comp.lang.python > #105382

Re: Static caching property

Path csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.freenet.ag!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Static caching property
Date Mon, 21 Mar 2016 11:48:27 -0600
Lines 35
Message-ID <mailman.453.1458582555.12893.python-list@python.org> (permalink)
References <35a5c4206a0c40e584d62d5d37b068b3@activenetwerx.com> <mailman.446.1458576961.12893.python-list@python.org> <56f0230e$0$1616$c3e8da3$5496439d@news.astraweb.com> <e043630368254fdeab16f0967df6662f@activenetwerx.com> <CAPTjJmqvu4qMpcbR-g5QZG6neuJVcHrKLYLDFe5QC_QBQbd2-g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 3U2qPxg31ESrejiiAlKJbQgIQMpjQkCArQBnIJBzDL6g==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'static': 0.03; '21,': 0.07; 'option,': 0.07; 'api': 0.09; '22,': 0.09; 'descriptor': 0.09; 'obj,': 0.09; 'def': 0.13; '10:54': 0.16; '2016': 0.16; 'deferred': 0.16; 'metaclass': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'refers': 0.18; "shouldn't": 0.18; '>>>': 0.20; 'class,': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'sense': 0.26; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'var': 0.27; 'callable': 0.29; "i'm": 0.30; 'skip:_ 10': 0.32; 'non': 0.32; 'expensive': 0.32; 'class': 0.33; 'foo': 0.33; 'instances': 0.33; 'option.': 0.33; 'tue,': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'clear': 0.35; 'protocol': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'end': 0.39; 'means': 0.39; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'mar': 0.65; 'to:name:python': 0.84; 'execution,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=ySwCrDb6do1oSIFyJH7ZleUpyX20x1OPUmAiq4j5hE8=; b=w6d/mOw7MLMT0MM+xOGnWHAST/lFrDBnb0QtfU3LHzrlUydgU3tR+5pWeFbl71k2vc HykhyR6FktPn3iZUMshWuw+LtC49QRkZ1Cvm7K/H0bXL6lKWAizL3LM8YURVW5jfJx9z 7qMRToj6YnuKYN4Zt46YnMzarVGI/55z9YWrHlZWyA4hr7+9bToAtqHlclaESBoHRI1y 8VCa3FhUOlzjYqW6QTbomEx2NfxaYXDqzClMq0cOIN4sxjysqNk/KYxW9tElv8Jc3Mga Ab4vfRfC7Kk6uSec/OGp7fg9cjvgS5JqPHNm6zuhYQv+E+CEP/rWrnMol5xZ8nQ9YiY6 o5uw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=ySwCrDb6do1oSIFyJH7ZleUpyX20x1OPUmAiq4j5hE8=; b=FjPASZzZEW+yUogj0FV5J1fDJySC7700yg+MHS3TjLkaIhwLSECWjl+v4no2tlbfzS me/pWybY0Bqo/fi6nVGv2fHyGK+f1z3XI5btWaGBzZ188RfniqpHzU9zKsiIL3e4HV48 Db4bkjbsGLo9liT02KzVpeTiTfuXS2WqexJxiAwn8/3JJWWE3hqSSj6jPzdVZsgGowz3 Jf242739S8chW7YHpcn6xY+SIFq18rWS2yzSuf/uoPDZFbJxrw6+Jp0uDBIZM+yFg8dp jko1RdTRp5S7khbyWe+iZlUHBd/X4WH4JovXpBHyY9uUDMlnBqPnuoNHOVYEo70QQPEI l+7Q==
X-Gm-Message-State AD7BkJI2dN9xTLG1NZDAG62pqw7+AaOG9O93KQ88z9C/BZl8eysBfKpWT8QGkWAB7V9gMkq9pTQgRuwGx2jcFw==
X-Received by 10.107.19.140 with SMTP id 12mr33671156iot.11.1458582547146; Mon, 21 Mar 2016 10:49:07 -0700 (PDT)
In-Reply-To <CAPTjJmqvu4qMpcbR-g5QZG6neuJVcHrKLYLDFe5QC_QBQbd2-g@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105382

Show key headers only | View raw


On Mon, Mar 21, 2016 at 10:54 AM, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Mar 22, 2016 at 3:49 AM, Joseph L. Casale
> <jcasale@activenetwerx.com> wrote:
>> Right, but _private refers to an api call that is expensive and may not even be accessed,
>> so while I may new up three instances of Test across a, b and c, if none of those end up
>> accessing var x, it shouldn't get fetched. Without some deferred execution, if the value
>> of _private is a callable whose return value is what I am interested in, it gets invoked the
>> moment the class is compiled.
>>
>> In the non static sense this is trivial to accomplish with the descriptor protocol, I am just not
>> clear for the static sense.
>
> One solution is to use descriptor protocol on the class, which means
> using a metaclass. I'm not sure it's the best option, but it is an
> option.

You don't actually need a metaclass for this:

>>> class Desc:
...     def __get__(self, obj, type=None):
...         if not type._cached_value:
...             type._cached_value = compute_value(type)
...         return type._cached_value
...
>>> def compute_value(x): return 42
...
>>> class Test:
...     foo = Desc()
...     _cached_value = None
...
>>> Test._cached_value
>>> Test.foo
42
>>> Test._cached_value
42

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


Thread

Re: Static caching property Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 10:15 -0600
  Re: Static caching property Steven D'Aprano <steve@pearwood.info> - 2016-03-22 03:36 +1100
    Re: Static caching property "Joseph L. Casale" <jcasale@activenetwerx.com> - 2016-03-21 16:49 +0000
    Re: Static caching property Chris Angelico <rosuav@gmail.com> - 2016-03-22 03:54 +1100
    Re: Static caching property "Joseph L. Casale" <jcasale@activenetwerx.com> - 2016-03-21 17:03 +0000
    Re: Static caching property Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 11:44 -0600
    Re: Static caching property Ethan Furman <ethan@stoneleaf.us> - 2016-03-21 10:45 -0700
    Re: Static caching property Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 11:48 -0600
      Re: Static caching property Steven D'Aprano <steve@pearwood.info> - 2016-03-22 11:05 +1100
        Re: Static caching property Chris Angelico <rosuav@gmail.com> - 2016-03-22 11:15 +1100
        Re: Static caching property Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-22 07:30 -0600

csiph-web