Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Lazy Attribute

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      11-16-2012
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
 
Reply With Quote
 
 
 
 
Stefan H. Holek
Guest
Posts: n/a
 
      11-16-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
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