Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Static caching property Date: Tue, 22 Mar 2016 07:30:33 -0600 Lines: 27 Message-ID: References: <35a5c4206a0c40e584d62d5d37b068b3@activenetwerx.com> <56f0230e$0$1616$c3e8da3$5496439d@news.astraweb.com> <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 DSoENgRLKJcdaBmTsaLkQw6JxC62bDskX/5lFSapHxxg== 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; 'received:209.85.223': 0.03; 'none,': 0.05; '21,': 0.07; 'cached': 0.09; 'descriptor': 0.09; 'none.': 0.09; 'obj': 0.09; 'obj,': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; '2016': 0.16; 'copy-pasted': 0.16; 'metaclass': 0.16; 'none).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds.': 0.16; 'sentinel': 0.16; 'supplying': 0.16; 'sure.': 0.16; 'threw': 0.16; 'wrote:': 0.16; 'first.': 0.18; 'am,': 0.23; 'this:': 0.23; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'compare': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'work.': 0.30; 'skip:_ 10': 0.32; 'expensive': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'protocol': 0.35; 'quite': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'there,': 0.37; 'signature': 0.37; "won't": 0.38; 'received:209': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'default': 0.61; 'mar': 0.65; 'believe': 0.66; '>>>>>': 0.66; 'to:name:python': 0.84 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=bZFjRVpfdL2zOcp3aoma44Uol84hBdbJhX0ChLabS6M=; b=l2qvPO7Mn6r0uGu64kz3db0Wf0aZbf8GMbfSeOWrfX+pzueutP9lfDdGRl7t2UH168 c0o/SBWc00Flh6XoArU8b5R224kTf+oYRqUKZ40c7H+2d/OmpgWiVZSXPEG+NGG02TBj ajbyHI9h7Gvg+LuvVNU5YGrHOajuHJZB2vJcVtSkua+J8OxGH1CPLMrjLGP2b5ZUFHig 4NamDY6fMZW2uAE1V1rOnVatzhM38qw/Iri9xiKMdQOgyLpnfMInXITZyWzQXCzHRYNv VIV8LnVNq++XO6GTINCIsceNpZmDyKQVfZwpWO8CnmzrCkDzNGysSOcs2nG6n6SuXaEL OcjA== 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=bZFjRVpfdL2zOcp3aoma44Uol84hBdbJhX0ChLabS6M=; b=a+08mJnJhIEy+gbt47yWJgpMZwMbywkyy2i/qvihGMfppyyycUuvmprHEKh5GKUpbt dYmyG2sIjRRMv1CTY0vc2OQ+kb7tW7pnbV3Quz4OReqCqKqW+vf8MkGjuYbEg2sdkITh Z4CP1N9fDwCNUQAlROcK6QgnDEfm7CPJcNyePh5BQCJwj0rbTqjVoAOfgjWFKUPa1Wd2 y2FGMSxeuu/IBt22xyT53JM5AMnZ3kkRlpN6GrfDdk2KL9pP18jkTgkz6lZEzc02AE3J u/CNz+RuvRuwO1mxrXsWL7l5/rzMforvBKXk16TuBA3mi32PkadJgS81PpfGsqQzQzmy YKnw== X-Gm-Message-State: AD7BkJJQPvQIryF5nDQJp2SHF408c1gyvboxtInqUEMQinv7D2Bv8RMa5R6cE7YefD9JjYIuujAj0J0So5h1XA== X-Received: by 10.107.19.140 with SMTP id 12mr38337825iot.11.1458653472743; Tue, 22 Mar 2016 06:31:12 -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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105482 On Mon, Mar 21, 2016 at 6:05 PM, Steven D'Aprano wrote: > On Tue, 22 Mar 2016 04:48 am, Ian Kelly wrote: > >> 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 > > > This won't quite work. What if the cached value happens to be falsey, yet > still expensive to compute? You should compare it to a known sentinel which > the expensive function will never return (possibly None). Or use a hasattr > test: if not hasattr(type, '_cached_value'). Sure. This was just a quick-and-dirty demonstration that I threw together in 30 seconds. > Also, you don't need the default type=None. The descriptor protocol should > never call __get__ without supplying the type. The obj may be None, but I > don't believe there are any circumstances where type will be None. Why do the examples in the Python docs use type=None? I literally just copy-pasted the method signature from there, since I can never remember which argument comes first.