Default parameters

Carl Banks imbosol at aerojockey.invalid
Thu Dec 18 12:26:49 CET 2003

Stian S?iland wrote:
> * J.R. spake thusly:
>> >    def f(d=[]):
>> >       d.append(0)
>> >       print d
>> >    f()
>> >    f()
>> > Explain results.  When is d bound?
> When is this issue going to be resolved? Enough newbie-pythoners have
> made this mistake now.
> Why not evaluate the parameter lists at calltime instead of definition
> time? This should work the same way as lambdas.

Consider something like this:

    def func(param=((1,2),(3,4),(5,6),(7,8))):

Do you really want to be building a big-ass nested tuple every time
the function is called?

Python evaluates default args at time of definition mostly for
performance reasons (and maybe so we could simulate closures before we
had real closures).  My gut feeling is, moving the evaluation to call
time would be too much of a performance hit to justify it.

CARL BANKS            
"If you believe in yourself, drink your school, stay on drugs, and
don't do milk, you can get work." 
          -- Parody of Mr. T from a Robert Smigel Cartoon

More information about the Python-list mailing list