Lazy argument evaluation (was Re: expression form of one-to-many dict?)

Steven Bethard steven.bethard at gmail.com
Tue Dec 21 15:50:19 EST 2004


Nick Coghlan wrote:
> 
> def lazy(x, *args, **kwds):
>   """Executes x(*args, **kwds) when called"""
>   if args or kwds:
>     return lambda : x(*args, **kwds)
>   else:
>     return x # No arguments, so x must be callable by itself
> 
> [snip]
> 
> Huh. I think I like the idea of lazy() much better than I like the 
> current PEP 312.

Well, 'lazy' is definitely much easier to read, reference in the 
documentation, etc. than ':' would be.

> There must be something wrong with this idea that I'm 
> missing. . .

Well, it does cost you some conciceness, as your examples show[1]:

lazy(mul, x, y)                     v.s.   :x * y
lazy(itemgetter(i), x)              v.s.   :x[i]
lazy(attrgetter("a"), x)            v.s.   :x.a
lazycall(lazy(attrgetter("a"), x))  v.s.   :x.a()

Not sure how I feel about this yet.  I don't personally need lazy 
argument evaluation often enough to be able to decide which syntax would 
really be clearer...

Steve

[1] To try to make things as fair as possible, I'm assuming that you've done
     from operator import mul, itemgetter, attrgetter
at the top of the module.



More information about the Python-list mailing list