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


Groups > comp.lang.python > #105381

Re: Static caching property

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ethan Furman <ethan@stoneleaf.us>
Newsgroups comp.lang.python
Subject Re: Static caching property
Date Mon, 21 Mar 2016 10:45:35 -0700
Lines 39
Message-ID <mailman.452.1458582319.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> <CAPTjJmqvu4qMpcbR-g5QZG6neuJVcHrKLYLDFe5QC_QBQbd2-g@mail.gmail.com> <89e865acae6941da978a0fb42c1df7b0@activenetwerx.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de kt6puKYjrzWEYwQlh8wD+QcPP0ldB61iFNqkaLbCAmEA==
Return-Path <ethan@stoneleaf.us>
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; 'none:': 0.05; 'option,': 0.07; '*args):': 0.09; '@property': 0.09; 'descriptor': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'def': 0.13; '(but': 0.15; 'cleaner': 0.16; 'complicating': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'class,': 0.22; 'am,': 0.23; 'slightly': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'wonder': 0.27; '~ethan~': 0.29; "i'm": 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'option.': 0.33; 'correctly': 0.34; 'that,': 0.34; 'instance': 0.35; 'protocol': 0.35; 'something': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'means': 0.39; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'accessed': 0.66; 'cheap...': 0.84; 'self.value': 0.84
User-Agent Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
In-Reply-To <89e865acae6941da978a0fb42c1df7b0@activenetwerx.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:105381

Show key headers only | View raw


On 03/21/2016 10:03 AM, Joseph L. Casale wrote:

>> 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.
>
> I will look at that, I wonder if however I am not over complicating it:
>
> class Foo:
>      _bar = None
>      @property
>      def expensive(self):
>          if Foo._bar is None:
>              import something
>              Foo._bar = something.expensive()
>          return Foo._bar
>
> Somewhat naive, but a test with if is pretty cheap...

A slightly cleaner approach (but only slightly):

   class Cache(object):
       _sentinal = object()
       def __init__(self, expensive_func):
           self.value = self._sentinal
           self.func = expensive_func
       def __get__(self, *args):
           if self.value is self._sentinal:
               self.value = self.func()
          return self.func()

The advantages:

- only one location in the class
- works correctly whether accessed via class or instance
- clue as to functionality in the name

--
~Ethan~

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