Newbie question - default values of a function
Matt Gerrans
matt.gerrans at hp.com
Wed Dec 22 17:29:44 EST 2004
Actually i was not mutable. Try this:
i = 1
id(i)
i += 1
id(i)
Change both of your sample functions to also print the id of the default
variable and that might shed a little more light on the matter.
"i = i + 1" is only changing the local reference called i to refer to an
instance of a different integer object. If you change the original
function to something similar, it will behave similarly:
def f( a, L=[]):
L = L + [a]
return L
Because you are assigning the local reference variable L to a new list,
instead of modifying that original default list that was created.
Is that more clear, or is it now more unclear? ;)
More information about the Python-list
mailing list