What's Going On?

Peter Otten __peter__ at web.de
Thu Mar 13 07:06:33 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

http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

You heard it through the grapevine ;)

Peter



More information about the Python-list mailing list