Re: Lazy Attribute

Discussion in 'Python' started by Steven D'Aprano, Nov 16, 2012.

  1. On Thu, 15 Nov 2012 15:46:19 -0700, Ian Kelly wrote:

    > 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.


    You just lost me right there. That's a poor UI design -- it violates the
    principle of least surprise. If I delete something, it should be deleted.
    Consider your example:

    >>>> del p.display_name
    >>>> p.display_name

    > 'Eliza Smith'


    That's very surprising. I am not aware of any other name in Python where
    deleting it does not remove the name from the namespace. (It is possible
    with properties, but I haven't ever come across someone who does that.)

    I don't have a good solution for invaliding such lazy attributes. Ideally
    we could have a new statement:

    refresh obj.attr # or some other name like "invalidate"

    but that won't happen. Other alternatives like:

    obj.attr.refresh()
    refresh(obj.attr)

    can't work because the function will see the result of the attribute
    lookup, not the lazy attribute itself. This won't do:

    obj.__class__.attr.refresh()

    because it won't know which instance to invalidate, although this could
    work:

    obj.__class__.attr.refresh(obj) # but it's ugly

    I'm very vaguely leaning towards this as the least-worst solution to
    invalidating the cached value:

    refresh(obj, 'attr') # pass the instance and the name


    --
    Steven
    Steven D'Aprano, Nov 16, 2012
    #1
    1. Advertising

  2. On 16.11.2012, at 11:29, Steven D'Aprano wrote:

    > I'm very vaguely leaning towards this as the least-worst solution to
    > invalidating the cached value:
    >
    > refresh(obj, 'attr') # pass the instance and the name


    This it exactly how lazy handles invalidation. http://lazy.readthedocs.org/en/latest/

    Stefan

    --
    Stefan H. Holek
    Stefan H. Holek, Nov 16, 2012
    #2
    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:
    661
    Steven D'Aprano
    Jan 16, 2009
  2. Boris Borcic
    Replies:
    0
    Views:
    539
    Boris Borcic
    Jan 16, 2009
  3. Boris Borcic
    Replies:
    0
    Views:
    537
    Boris Borcic
    Jan 16, 2009
  4. Ian Kelly

    Re: Lazy Attribute

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

    RE: Lazy Attribute

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

Share This Page