[Python-3000] Wild idea: Deferred Evaluation & Implicit Lambda

Josiah Carlson jcarlson at uci.edu
Wed May 31 02:52:59 CEST 2006


Alexander Belopolsky <alexander.belopolsky at gmail.com> wrote:
> 
> Josiah Carlson <jcarlson <at> uci.edu> writes:
> 
> > 
> > 
> > Talin <talin <at> acm.org> wrote:
> > > 
> > > I want to take a stab at unifying a number of ideas that have been 
> > > proposed recently:
> > 
> > 1. It's either a strange way of spelling lambda, and doesn't gain
> > anything except a reduction in typing by 1 character.
> > 2. Or it's a strange way of deferring evaluation of a line in the
> > current stackframe.
> 
> My understanding of Talin's proposal is that it is # 2 above. I don't see
> anything strange about this idea.  In fact the S family of languages (S,
> S-plus, R) defers evaluation of function arguments by default.  This
> feature has some interesting concequences, for example R's plot function
> can put "y" on the label when invoked as plot(y).
> 
> I don't think python needs any syntactic sugar to support this feature.
> One can emulate R's lazy evaluation by implementing a "promise" class that
> contains slots for a callable and a result that is filled on demand.
> 
> Something along the following lines:
[snip]
> Similarly, one can write "promise_number", "promise_sequence" etc. classes
> that will dispatch the appropriate special methods to a lazily evaluated
> object.  Once type annotations are in-place, it may even become possible
> to spell such classes "promise[interface]" and automate their creation.
> I think this is a valuable use case for the future type annotation
> discussions.

Really, this boils down to another way of spelling lambda.

    a.default = lambda: b.get(key, 0)
    value = a[key]

Changing the __dict__ on an instance gives you equivalent behavior for
attribute access.  Before I posted my last response, I hacked together a
generator wrapper, but then I noted that in the 6 or so lines it took me
to properly specify a lazy dict getter, it was all essentially a change
in the default of a default dictionary.  I'm still not convinced that
'lazy <expression>' or your equivalent promise class are necessary or
desireable for the evolution of Python; especially given the simplicity
of the lambda solution.


 - Josiah



More information about the Python-3000 mailing list