Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Lazy Attribute

Reply
Thread Tools

RE: Lazy Attribute

 
 
Andriy Kornatskyy
Guest
Posts: n/a
 
      11-16-2012

Ian,

Thank you for mentioning about this research, really appreciate that.

Thanks.

Andriy Kornatskyy


----------------------------------------
> From: http://www.velocityreviews.com/forums/(E-Mail Removed)
> Date: Thu, 15 Nov 2012 15:46:19 -0700
> Subject: Re: Lazy Attribute
> To: (E-Mail Removed)
>
> On Thu, Nov 15, 2012 at 12:33 PM, Andriy Kornatskyy
> <(E-Mail Removed)> 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/...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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
lazy evaluation is sometimes too lazy... help please. Ken Pu Python 3 01-16-2009 11:23 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:46 AM
Re: lazy evaluation is sometimes too lazy... help please. Boris Borcic Python 0 01-16-2009 10:37 AM
hello! first post to clr. I'm asking about an attempt at a lazy rubysolution to computing fibonacci numbers for a project euler problem. seems tobe a bug in lazy ruby... tphyahoo Ruby 6 08-08-2008 08:15 PM



Advertisments