[Python-Dev] lazy evaluation redux (was dict "setdefault". Feaure request or bugfix?)

Alex Martelli aleax@aleax.it
Tue, 11 Feb 2003 15:44:12 +0100


On Tuesday 11 February 2003 03:17 pm, Roman Suzi wrote:
> On Tue, 11 Feb 2003, Alex Martelli wrote:
> > E.g., a strawman syntax might be...:
> >
> > def setdefault(adict, akey, ?avalue):
> >     if akey not in adict:
> >         adict[akey] = evaluate_now(avalue)
> >     return adict[akey]
> >
> > to be called as, e.g.
> >
> > setdefault(mydict, 'goo', ?makeavalue(x))
> >
> > this would use ? for both formal and actual arguments to
> > mean lazy evaluation, and a new builtin to force the time
> > of evaluation.  Other choices are, of course, possible.
>
> This could be good for the properties as well.
> However I think omitted lambda is better and nicer:
>
> def setdefault(adict, akey, avalue):
>      if akey not in adict:
>          adict[akey] = avalue(args...)
>      return adict[akey]

<blink> I don't get it -- where are "args" coming from?

I do see that if a "lazily evaluated parameter" is just a
callable-without-arguments, then "evaluate_now" easily
collapses to "apply" (or the () operator) and no syntax is 
needed at the called-site.  I think I'd rather _have_ the
called-site syntax, but your proposal's simplicity of
implementation does help, of course.

> Alex, are we up to make a PEP for this?
> (it's pretty simple change to grammar: no need to change anything else,
> probably we can convince Guido to accept it due to simplicity.)

Yes, these are the pluses.  However, the PEP should also point out
the problems, i think -- the scarce visibility of that leading ":" and the
lack of any marker at the called-point (for arguments).  Hmmm....

> Horizonts for 0-arity lambdas are numerous.
>
> The idea is that "lambda" keyword is optional in some circumstances, like:
>
> - as an argument in function call
> - right after "=" in assignment
> - in default value specification (in def statement)
> (some other cases)
>
>
> inline if then will be something like:
>
> def iif(cond, true, false):
>   if cond: return true()
>   else: return false()
>
> iif(cond, :1, :2)
>
> This will clearly indicate to programmer that values are "quoted" (like
> in LISP) - not evaluated right now.

This is a nice use case (even though I'd call the function ifelse;-).  And
yes, thinking of that leading : as "quoting" does help.  Double hmmm...


Alex