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


Groups > comp.lang.python > #33402

Re: Lazy Attribute

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
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.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'error:': 0.05; 'welcome.': 0.07; 'python': 0.09; '@property': 0.09; 'descriptor': 0.09; 'itself,': 0.09; 'def': 0.10; '33,': 0.16; 'class:': 0.16; 'descriptors': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '>>>': 0.18; 'trying': 0.21; 'import': 0.21; 'do.': 0.21; '"",': 0.22; '15,': 0.23; 'properties': 0.24; 'header:In-Reply- To:1': 0.25; '(most': 0.27; 'message-id:@mail.gmail.com': 0.27; 'post': 0.28; 'once.': 0.29; 'class': 0.29; 'received:209.85.215.46': 0.30; 'url:2012': 0.30; 'file': 0.32; 'comments': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'url:blogspot': 0.64; 'results': 0.65; 'url:11': 0.71; 'special': 0.73; 'to:name:python': 0.84; 'demand': 0.96
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 :content-type; bh=5Ccfa+PduxdjTFRLsW8jiP+mls1GTo3wGqkPAVwx9pk=; b=gvfNk1A/odn46voyfgH4LpmvA5asWQFISExj8GAzuF9TCrrBKWrh/eaK0xG1tG705E BKlLe+KmYmPnYhOIxBfZEDq9PhHF+kUy8wMokiQrtORYm5nS24ZV8yV8mPP8Rnvy22lV MC/DuUBuJyXQe31WCTY3hQawT9K9YsWBh8itbp05Lt5ujP7oggzMjTgN1eQmNhgqpJm9 hCclwQBCPTb054HLoIJKqgw5xUnz9DYVEHCpBDG/fb6XgcNZ6ZHq02VSdl10deyybiab RawxgNFqka7eTUPtSpYGPFPUmuxsCEOWmY9wA+HIxSUDBnKqkZX8pjBewBEXTIPt3hQs rrjw==
MIME-Version 1.0
In-Reply-To <DUB117-W881D7D74D529D159D8B33F91520@phx.gbl>
References <DUB117-W881D7D74D529D159D8B33F91520@phx.gbl>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 15 Nov 2012 15:24:40 -0700
Subject Re: Lazy Attribute
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3728.1353018314.27098.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353018314 news.xs4all.nl 6955 [2001:888:2000:d::a6]:50260
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33402

Show key headers only | View raw


On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy
<andriy.kornatskyy@live.com> wrote:
>
> A lazy attribute is an attribute that is calculated on demand and only once.
>
> The post below shows how you can use lazy attribute in your Python class:
>
> http://mindref.blogspot.com/2012/11/python-lazy-attribute.html
>
> Comments or suggestions are welcome.

The name "attribute" is not very descriptive.  Why not "lazy_attribute" instead?

I note that trying to access the descriptor on the class object
results in an error:

>>> from descriptors import attribute
>>> class Foo:
...     @attribute
...     def forty_two(self):
...         return 6 * 9
...
>>> Foo().forty_two
54
>>> Foo.forty_two
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\descriptors.py", line 33, in __get__
    setattr(obj, f.__name__, val)
AttributeError: 'NoneType' object has no attribute 'forty_two'

If accessing the descriptor on the class object has no special
meaning, then the custom is to return the descriptor object itself, as
properties do.

>>> class Foo:
...     @property
...     def forty_two(self):
...         return 6 * 9
...
>>> Foo().forty_two
54
>>> Foo.forty_two
<property object at 0x0280AD80>

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


Thread

Re: Lazy Attribute Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-15 15:24 -0700

csiph-web