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

Lalo Martins lalo@laranja.org
Tue, 11 Feb 2003 12:37:06 -0200


On Tue, Feb 11, 2003 at 02:33:45PM +0100, Alex Martelli wrote:
> 
> 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.

+0.5

when nobody is looking I smirk at the fact that tcl has a feature python
doesn't ;-) (well, not really... it could be argued our lambda does the same
thing better)

Instead of a new builtin, I think the "lazy evaluation block" should be an
object; you can then call a method, in your example avalue.eval()

What happens if you don't add the ? to the call?  setdefault(mydict, 'foo', v)

IMHO it would still build a "lazy evaluation block" but somehow "already
evaluated".

One more important bit: what happens if you call .eval() multiple times?  Do
each one result in a new evaluation, or is the result cached internally?  I
can see use cases for both.  Perhaps ruling that each .eval() re-evaluates
the block works - then if you want to avoid it, just assign the result to a
local variable (explicit).


OTOH, off the top of my head I can't think of another valid use case for
this whole feature other than setdefault.

[]s,
                                               |alo
                                               +----
--
            Those who trade freedom for security
               lose both and deserve neither.
--
http://www.laranja.org/                mailto:lalo@laranja.org
         pgp key: http://www.laranja.org/pessoal/pgp

Eu jogo RPG! (I play RPG)         http://www.eujogorpg.com.br/
GNU: never give up freedom                 http://www.gnu.org/