Default parameters

Carl Banks imbosol at aerojockey.invalid
Sat Dec 20 12:55:27 EST 2003


Rainer Deyke wrote:
> 
> 
> Carl Banks wrote:
>> Now, do have any evidence that non-constant, default arguments (as
>> they are now) are USEFUL?
> 
> def draw_pixel(x, y, color, surface=screen):
>  screen[y][x] = color

Was that so hard?  (Although this would still work if surface was
evaluated call time.)


>> (I don't agree with your "consistent" theory, anyways.  The function
>> would be treating all arguments consistently: it's just that it'd get
>> a fresh copy of the default arguments each call.)
> 
> A function that mutates its arguments should not be called with "fresh"
> arguments, implicitly or explicitly.  If the purpose of the function is to
> modify its arguments, then doing so would throw away the effect of the
> function.  If the purpose of the function is not to modify its arguments,
> then it shouldn't do so.


In the function:

    def a(b=[]):
        pass

b=c is either part of the function, or part of the definition.  If
it's part of the definition, it gets evaulated once, and the function
gets the same object each time.  If it's part of the function, it gets
evaluated every call, and b gets a new list every time.  Either way is
consistent.

You ideas about what a function's purpose is are just not relevant to
whether the time of evaluation is consistent.


-- 
CARL BANKS                      http://www.aerojockey.com/software
"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