Newbie question - default values of a function

Fredrik Lundh fredrik at pythonware.com
Wed Dec 22 17:26:07 EST 2004


"Bulba!" wrote:

> Surely the variable i is a mutable object?

nope.

> OK, again:
>
>>>> def t(a,i=[0]):
> ... i[0]=i[0]+1
> ... return (a, i)
> ...
>>>> t(1)
> (1, [1])
>>>> t(3)
> (3, [2])
>>>> t(5)
> (5, [3])
>
> Yes, it works with the list. But why sharing value between calls
> pertains some mutable objects and not the other mutable objects?

a variable is not an object.

reset your brain, and read this:

    http://www.effbot.org/zone/python-objects.htm

or the longer version:

    http://starship.python.net/crew/mwh/hacks/objectthink.html

</F> 






More information about the Python-list mailing list