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

Jesus Cea Avion jcea@argo.es
Tue, 11 Feb 2003 16:30:48 +0100


> One way to satisfy this would be to redefine setdefault something like
> this:
> 
> def setdefault(aDict, aKey, aValue=None, lazy=None):
>    assert aValue is None or lazy is None
>    if aKey not in aDict:
>        aDict[aKey] = (if lazy is None: aValue else: lazy())
>    return aDict[aKey]

Nice. Nevertheless I keep thinking that current "setdefault" behaviour
is not fully documented and counterintuitive :-). So we would need to
fix the behaviour or update docs :-p

God, you all mutate a fairly simple and concrete consideration into a
broad language "lazy evaluation" feature. I'm overhelmed };-)

>    myDict.setdefault(myKey, lazy=lambda:expensive_function(args))

Nice lambda usage: declare a pseudofunction that calls another function
with predefined parameters. Updating my toolbox... :)

-- 
Jesus Cea Avion                         _/_/      _/_/_/        _/_/_/
jcea@argo.es http://www.argo.es/~jcea/ _/_/    _/_/  _/_/    _/_/  _/_/
                                      _/_/    _/_/          _/_/_/_/_/
PGP Key Available at KeyServ   _/_/  _/_/    _/_/          _/_/  _/_/
"Things are not so easy"      _/_/  _/_/    _/_/  _/_/    _/_/  _/_/
"My name is Dump, Core Dump"   _/_/_/        _/_/_/      _/_/  _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibniz