Python bug with [] as default value in functions?

Robin Becker robin at jessikat.fsnet.co.uk
Thu Apr 12 08:55:04 EDT 2001


In article <3AD5989D.E22E2CBF at darwin.in-berlin.de>, Dinu Gherman
<gherman at darwin.in-berlin.de> writes
...
Hi I think you are getting involved with one of the standard 'features'
of python.

eg
>>> def dingo(n,L=[]):
...     if n: L.append(n)
...     return L
... 
>>> dingo(1)
[1]
>>> dingo(0)
[1]
>>>

ie the first call of dingo modified the default argument. Since the
empty list is mutable that particular version of it gets modified by the
first call and changes the default for the second.

Hope your tooth ache is better. 
-- 
Robin Becker



More information about the Python-list mailing list