Delayed evaluation and setdefault()

Erik Max Francis max at
Mon Jan 19 20:54:20 CET 2004

Leo Breebaart wrote:

> So I guess my question is twofold: one, do people think
> differently, and if so why?; and two: is there any elegant (or
> other) way in which I can achieve the delayed evaluation I desire
> for setdefault, given a side-effect-having b()?

The setdefault method of dictionaries is just a normal method in Python;
only the builtin `and' and `or' operators have special short-circuiting
behavior.  That is to say, there are no builtin functions or methods in
Python which are "special forms."

It's true that the kind of thing you want to do (lazy evaluation in some
incidental context) is common, but it would not be a good idea to start
selectively adding special forms to the language since it would greatly
complicate things.  Right now if I see

	this.that(something, theOther())

I can immediately tell what gets evaluated or called and when.  I don't
need to know that there are special cases in some things that look like
method/function calls but actually might be special forms, and that's a
good thing.

As for your particular solution, you're much better off implementing the
lazy evaluation you want yourself, rather than wishing there were
special language support for it for that particular method (or a very
small subset of methods/functions).

 __ Erik Max Francis && max at &&
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ The critical period in matrimony is breakfast-time.
    -- A.P. Herbert

More information about the Python-list mailing list