What's Going On?

Diez B. Roggisch deets at nospam.web.de
Thu Mar 13 07:12:37 EDT 2008


MartinRinehart at gmail.com wrote:

> (Accompanied by Marvin Gaye)
> 
>>>> def f(list=[0]):
> ...    list[0]+=1
> ...    return list[0]
> ...
>>>> f()
> 1
>>>> f()
> 2
>>>> f() # 'list' is a name bound to a list (mutable) so this makes sense
> 3
>>>> f([5])
> 6
>>>>f() # What's Going On?
> 4

That the same default argument is mutated? What did you expect, that it got
replaced by you passing another list? That would kind of defy the meaning
of default-arguments, replacing them whenever you call a function with
actual parameters.


Diez



More information about the Python-list mailing list