[Python-Dev] lazy evaluation redux (was dict "setdefault". Feaure
request or bugfix?)
Roman Suzi
rnd@onego.ru
Tue, 11 Feb 2003 17:17:33 +0300 (MSK)
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]
>
> to be called as, e.g.
>
setdefault(mydict, 'goo', :makeavalue(x))
equal to
setdefault(mydict, 'goo', lambda:makeavalue(x))
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.)
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.
Sincerely yours, Roman A.Suzi
--
- Petrozavodsk - Karelia - Russia - mailto:rnd@onego.ru -