pitfall for your amusement

Erik Max Francis max at alcyone.com
Tue Nov 12 22:11:58 EST 2002


Terry Hancock wrote:

> Hmm. I don't see this:
> 
> >>> def spam(eggs, ham=[]):
> ...     print "%s and %s" % (eggs, ham)
> ...
	...
> What would I have to do for this to be a problem? (as it happens, I
> have used
> this kind of default before, so I would like to understand the
> pitfall!)

If that list is manipulated in some way, or is returned to the caller
(where it can be manipulated), then that makes the difference:

>>> def f(x, l=[]):   
...  l.append(x)
...  print l      
... 
>>> f(1)
[1]
>>> f(2)
[1, 2]
>>> f(3)
[1, 2, 3]
>>> f(4)
[1, 2, 3, 4]

It's the same list object even that's probably not what you meant.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I will always remember / This moment
\__/ Sade
    Kepler's laws / http://www.alcyone.com/max/physics/kepler/
 A proof of Kepler's laws.



More information about the Python-list mailing list