Empty list as default parameter

Paul Rubin http
Sun Nov 23 01:06:03 EST 2003


bokr at oz.net (Bengt Richter) writes:
> >That's always seemed like a source of bugs to me, and ugly workarounds
> >for the bugs.  Is there any corresponding disadvantage to eval'ing the
> >defaults at runtime as is done in other languages?

> What languages are you thinking of? A concrete example for
> comparison would clarify things. 

Common Lisp is the most obvious one.

> Would you have default expressions effectively passed in as the
> bodies of lambdas (which might mean creating closures, depending on
> what was referenced) and then executed to create the local bindings
> prior to the first line in a function or method?  It would certainly
> be inefficient for all the cases where you just wanted a static
> default ...

The compiler can do the obvious things to make efficient code in the
normal cases.

> (unless you special cased those to work as now -- but
> remember, only bare names and constant literals could be special
> cased that way. An expression like os.RD_ONLY (yes that is an
> expression!) would have to be passed as lambda: os.RDONLY). So you'd
> have to counter that by making bare-name bindings prior to calls,
> like tmp_mode=os.RD_ONLY; os.open('foo.txt', tmp_mode); #etc

Rather than have the programmer go through such contortions it's better
fix the compiler to generate the obvious code inline, and then rely
on the compiler to get these things right.




More information about the Python-list mailing list