Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33416
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <andriy.kornatskyy@live.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'cache': 0.05; 'try:': 0.07; 'welcome.': 0.07; 'wrapper': 0.07; 'python': 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; '@property': 0.09; 'descriptor': 0.09; 'def': 0.10; 'attribute,': 0.16; 'class:': 0.16; 'email addr:functools.wraps(func)': 0.16; 'foo()': 0.16; 'lazily': 0.16; 'wrote:': 0.17; 'replacing': 0.17; 'thu,': 0.17; '>>>': 0.18; 'to:name:python-list@python.org': 0.20; 'skip:- 40': 0.21; 'thanks.': 0.21; 'keyerror:': 0.22; 'example': 0.23; 'programming': 0.23; '15,': 0.23; 'to:2**1': 0.23; 'header:In- Reply-To:1': 0.25; 'post': 0.28; 'email name:': 0.29; 'faster,': 0.29; 'once.': 0.29; 'date:': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; 'that.': 0.30; 'usually': 0.30; 'url:2012': 0.30; 'function': 0.30; 'url:python': 0.32; 'url:listinfo': 0.32; 'comments': 0.33; 'from:addr:live.com': 0.33; 'to:addr:python-list': 0.33; 'that,': 0.34; 'nov': 0.35; 'pm,': 0.35; 'add': 0.36; 'really': 0.36; 'subject:': 0.36; 'except': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'should': 0.36; 'thank': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'from:': 0.38; 'object': 0.38; 'some': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'your': 0.60; 'easy': 0.60; 'email name:python-list': 0.62; 'email addr:gmail.com': 0.63; 'url:blogspot': 0.64; 'taking': 0.65; 'research,': 0.71; 'url:11': 0.71; 'entry,': 0.84; 'ian,': 0.84; 'demand': 0.96; 'charset:windows-1251': 0.97 |
| X-Originating-IP | [194.44.213.194] |
| From | Andriy Kornatskyy <andriy.kornatskyy@live.com> |
| To | <ian.g.kelly@gmail.com>, "python-list@python.org" <python-list@python.org> |
| Subject | RE: Lazy Attribute |
| Date | Fri, 16 Nov 2012 10:50:56 +0300 |
| Importance | Normal |
| In-Reply-To | <CALwzidnsKKk1w83JX97rZJcTfhcN6QP4o=M_FM8tXxKyQW7fBg@mail.gmail.com> |
| References | <CALwzidnsKKk1w83JX97rZJcTfhcN6QP4o=M_FM8tXxKyQW7fBg@mail.gmail.com> |
| Content-Type | text/plain; charset="windows-1251" |
| Content-Transfer-Encoding | quoted-printable |
| MIME-Version | 1.0 |
| X-OriginalArrivalTime | 16 Nov 2012 07:50:56.0603 (UTC) FILETIME=[1C6AF2B0:01CDC3CF] |
| 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.3735.1353052323.27098.python-list@python.org> (permalink) |
| Lines | 94 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1353052323 news.xs4all.nl 6951 [2001:888:2000:d::a6]:40702 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:33416 |
Show key headers only | View raw
Ian,
Thank you for mentioning about this research, really appreciate that.
Thanks.
Andriy Kornatskyy
----------------------------------------
> From: ian.g.kelly@gmail.com
> Date: Thu, 15 Nov 2012 15:46:19 -0700
> Subject: Re: Lazy Attribute
> To: python-list@python.org
>
> 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.
>
> I should add that I like the approach you're taking here. Usually
> when I want a lazy property I just make an ordinary property of a
> memoized function call:
>
> def memoize(func):
> cache = {}
> @functools.wraps(func)
> def wrapper(*args, **kwargs):
> kwset = frozenset(kwargs.items())
> try:
> return cache[args, kwset]
> except KeyError:
> result = cache[args, kwset] = func(*args, **kwargs)
> return result
> return wrapper
>
> class Foo:
> def __init__(self):
> self.times_called = 0
>
> @property
> @memoize # Alternatively, use functools.lru_cache
> def forty_two(self):
> self.times_called += 1
> return 6 * 9
>
>
> >>> foo = Foo()
> >>> foo.times_called
> 0
> >>> foo.forty_two
> 54
> >>> foo.times_called
> 1
> >>> foo.forty_two
> 54
> >>> foo.times_called
> 1
>
>
> Although you don't go into it in the blog entry, what I like about
> your approach of replacing the descriptor with an attribute is that,
> in addition to being faster, it makes it easy to force the object to
> lazily reevaluate the attribute, just by deleting it. Using the
> Person example from your blog post:
>
> >>> p = Person('John', 'Smith')
> >>> p.display_name
> 'John Smith'
> >>> p.display_name
> 'John Smith'
> >>> p.calls_count
> 1
> >>> p.first_name = 'Eliza'
> >>> del p.display_name
> >>> p.display_name
> 'Eliza Smith'
> >>> p.calls_count
> 2
>
> Although in general it's probably better to use some form of reactive
> programming for that.
> --
> http://mail.python.org/mailman/listinfo/python-list
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
RE: Lazy Attribute Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-16 10:50 +0300
csiph-web