Default parameters
Terry Reedy
tjreedy at udel.edu
Sun Dec 21 11:47:42 EST 2003
"Carl Banks" <imbosol at aerojockey.invalid> wrote in
message
news:KKPEb.22919$031.11381 at fe3.columbus.rr.com...
> >> DEFAULT_LIST = ((1,2),(3,4),(5,6),(7,8))
> >> def func(param=DEFAULT_LIST):
> >> pass
> >>The compiler couldn't optimize any of the
above cases.
> >
> > For the DEFAULT_LIST (tuple?) and that
particular dict literal, why not?
> Well, the value of DEFAULT_LIST is not known a
compile time
To further illustrate the problem with the
wished-for change, consider:
DL = (1,2)
def f(x=DL): print x
[lots of code]
DL = (3,4)
f()
What should this print? Currently, (1,2). To
get, (3,4) instead, one can default x to None and
dynamically reset x in the body to the calltime
value of DL. If default expressions were
evaluated at runtime, the choice of fixing the
default at definition, regardless of subsequent
code, would, as near as I can think of at the
moment, be excluded.
Terry J. Reedy
More information about the Python-list
mailing list