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


Groups > comp.lang.python > #105382

Re: Static caching property

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Static caching property
Date 2016-03-21 11:48 -0600
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>

Show all headers | 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