RE: Lazy Attribute

Discussion in 'Python' started by Andriy Kornatskyy, Nov 16, 2012.

  1. Ian,

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

    Thanks.

    Andriy Kornatskyy


    ----------------------------------------
    > From:
    > Date: Thu, 15 Nov 2012 15:46:19 -0700
    > Subject: Re: Lazy Attribute
    > To:
    >
    > On Thu, Nov 15, 2012 at 12:33 PM, 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 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
     
    Andriy Kornatskyy, Nov 16, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Pu
    Replies:
    3
    Views:
    674
    Steven D'Aprano
    Jan 16, 2009
  2. Boris Borcic
    Replies:
    0
    Views:
    550
    Boris Borcic
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    547
    Boris Borcic
    Jan 16, 2009
  4. Ian Kelly

    Re: Lazy Attribute

    Ian Kelly, Nov 15, 2012, in forum: Python
    Replies:
    0
    Views:
    175
    Ian Kelly
    Nov 15, 2012
  5. Andriy Kornatskyy

    RE: Lazy Attribute

    Andriy Kornatskyy, Nov 16, 2012, in forum: Python
    Replies:
    3
    Views:
    155
    Rouslan Korneychuk
    Nov 16, 2012
Loading...

Share This Page