![]() |
Available news archives:
comp.lang.tcl
-
comp.lang.python
-
comp.security.firewalls
-
sci.crypt -
comp.lang.php -
comp.lang.javascript
|
|
comp.lang.python archiveRe: evaluated function defaults: stored where?
From: alex23 <wuwei23@gmail.com>
Date: Thu May 26 2005 - 08:14:10 CEST
David Isaac wrote:
It can be even more surprising if a default value is mutable:
>>> def foo(a, b=[]):
>>> foo(3,[1,2])
>>> foo('c',['a','b'])
>>> foo(1)
So far, everything is behaving much as you'd expect, but then:
>>> foo(2)
>>> foo(3)
The parameter is bound to the list at creation time and, being mutable,
This can be avoided by modifying your function slightly:
>>> foo(3, [1,2])
>>> foo('c', ['a','b'])
>>> foo(1)
>>> foo(2)
>>> foo(3)
-alex23
|