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


Groups > comp.lang.python > #105419

Re: Static caching property

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Static caching property
Date Tue, 22 Mar 2016 11:15:49 +1100
Lines 31
Message-ID <mailman.474.1458605752.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> <mailman.453.1458582555.12893.python-list@python.org> <56f08c59$0$1590$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 5DJEJ5OU2kkBqKomfnV0kAXmWRi7Dg+C9GdTRSF/pjzA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'cc:addr:python-list': 0.09; '22,': 0.09; 'obj,': 0.09; 'def': 0.13; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sentinel': 0.16; 'wrote:': 0.16; 'skip': 0.18; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:_ 10': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'mar': 0.65; 'of:': 0.66; 'guaranteed': 0.67; 'chrisa': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=Vj7CaWK3lTi/n1Atz5fxagQZPQ2a4GoIqA3JM7KdPu4=; b=VUKwRjtO6eqsI4uXAgmXBA9cA8hy9/0C/avn1TLLWs+VPXaEVROfC7fCtgy3Ox85Z1 fXWjnf6PZOYm7b+pUUWv3H5k0EddUSr3t3DknqOFg4mQjpL3i3tL6qY2n5qNZ7QWFLqi i3m+v5ncOKcCmzlPqVmhcWqhM8OgRr/HA9GzPzMqIN00yD9pFzhVGfwKTct/TH8rKk6w rsDNgDHlVVjQMXmt8OvPc2xd1pbVN8KiAqoMgHw1JxAkq49xfkRS7BStBKgW9bf+TcIV yybJrvVjuQFPrXEN4LGkkKGV+YP88jsM+ZMiDA07cQCIZiG5zOzozG+6ixA0CYzety7w 7jhA==
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:date :message-id:subject:from:cc; bh=Vj7CaWK3lTi/n1Atz5fxagQZPQ2a4GoIqA3JM7KdPu4=; b=KfMpm7CyByVoKySlWbXl1/aycdgCxq1frMFnxdSDQi+RFEhspIdccnhrK/Ll+DgIT8 aXcGwnuw2yZzcXtQVvaKlTAUMwRysyXKrCk8RlG2v9c5cPXMUxB8XX+lHkdrxGEZVru2 iGq7mXeMsGRQ5JfgtZwEvdc8ZAsDjYAk6I1ebsSBfqjkhvGJRN5Dq7SSObg2kV5gffHR eYrdmik4fvjmNxEyzl8zTdCY5YDxTxxyIHJZB74Qw3esen3HoE4UN97VuH4aqW6+Olu0 rdTdgyFbWYfSISYqHEzIOgf5ZoOwZAmGQY+gkS8npH9EdceVMCgfYG9f2uPlTF7YWKqR dzrg==
X-Gm-Message-State AD7BkJKXo2/2raOboT90acU7KCCrJHEXUsD/oRZV4GzufNy0LEShSWCtQnqfFnluG5wYCSH/7CoOqigzu6TqNw==
X-Received by 10.107.63.139 with SMTP id m133mr28893974ioa.157.1458605749465; Mon, 21 Mar 2016 17:15:49 -0700 (PDT)
In-Reply-To <56f08c59$0$1590$c3e8da3$5496439d@news.astraweb.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:105419

Show key headers only | View raw


On Tue, Mar 22, 2016 at 11:05 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> But my favourite is to combine them:
>
>
> class Desc:
>     def __get__(self, obj, type):
>         sentinel = object()  # guaranteed to be unique
>         value = getattr(type, _cached_value, sentinel)
>         if value is sentinel:
>             value = type._cached_value = compute_value(type)
>         return value
>
>
>

That seems like overkill. Inside getattr is the equivalent of:

try: return type._cached_value
except AttributeError: return sentinel

So skip getattr/hasattr and just use try/except:

class Desc:
    def __get__(self, obj, type):
        try:
            return type._cached_value
        except AttributeError:
            value = type._cached_value = compute_value(type)
            return value

ChrisA

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