[Python-ideas] fixing mutable default argument values

Chris Rebert cvrebert at gmail.com
Sun Jan 21 07:09:46 CET 2007

Josiah Carlson wrote:
> As provided by Calvin Spealman, the above can be fixed with:
>     def popo(x=None):
>         x = x if x is not None else []
>         x.append(666)
>         print x
> I would also mention that forcing users to learn about mutable arguments
> and procedural programming is not a bad thing.  Learning the "gotcha"
> of mutable default arguments is a very useful lesson, and to remove that
> lesson, I believe, wouldn't necessarily help new users to Python, or new
> programmers in general.
>  - Josiah
First, your 'fix' misses the point: though the proposed feature isn't 
necessary, and code can be written without using it, it allows mutable 
default argument values to be expressed more clearly and succinctly than 
the idiom your 'fix' uses. Second, Python isn't (inherently) about 
teaching new programmers about programming, and what is good for newbies 
isn't necessarily good for experienced programmers. And at any rate, the 
lesson would still exist in the form of having to use the new feature I 
proposed (in my strawman syntax, 
<DefaultArgumentValueThatGetsReEvaluated()>), and also in doing 
multiplication on 2D lists (e.g. x = [mutable]*42; x[7].mutate(); 
x[0].mutated == True).

Comments (from anyone) on my revised proposal (from the 2nd email)? I 
would particularly appreciate alternate syntax suggestions.

- Chris Rebert

More information about the Python-ideas mailing list