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: 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 To: , "python-list@python.org" Subject: RE: Lazy Attribute Date: Fri, 16 Nov 2012 10:50:56 +0300 Importance: Normal In-Reply-To: References: , 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 Ian=2C Thank you for mentioning about this research=2C really appreciate that. Thanks. Andriy Kornatskyy ---------------------------------------- > From: ian.g.kelly@gmail.com > Date: Thu=2C 15 Nov 2012 15:46:19 -0700 > Subject: Re: Lazy Attribute > To: python-list@python.org > > On Thu=2C Nov 15=2C 2012 at 12:33 PM=2C Andriy Kornatskyy > 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 clas= s: > > > > 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 =3D {} > @functools.wraps(func) > def wrapper(*args=2C **kwargs): > kwset =3D frozenset(kwargs.items()) > try: > return cache[args=2C kwset] > except KeyError: > result =3D cache[args=2C kwset] =3D func(*args=2C **kwargs) > return result > return wrapper > > class Foo: > def __init__(self): > self.times_called =3D 0 > > @property > @memoize # Alternatively=2C use functools.lru_cache > def forty_two(self): > self.times_called +=3D 1 > return 6 * 9 > > > >>> foo =3D 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=2C what I like about > your approach of replacing the descriptor with an attribute is that=2C > in addition to being faster=2C it makes it easy to force the object to > lazily reevaluate the attribute=2C just by deleting it. Using the > Person example from your blog post: > > >>> p =3D Person('John'=2C 'Smith') > >>> p.display_name > 'John Smith' > >>> p.display_name > 'John Smith' > >>> p.calls_count > 1 > >>> p.first_name =3D '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 =