<html>
<body>
At 01:39 PM 6/26/2011, Shashank Singh wrote:<br>
<blockquote type=cite class=cite cite="">On Sun, Jun 26, 2011 at 11:58
PM, Marc Aymerich <glicerinu@gmail.com> wrote:<br>
> Hi,<br>
> I'm trying to define a function that has an optional parameter
which<br>
> should be an empty list whenever it isn't given. However, it takes
as<br>
> value the same value as the last time the function was executed.
What<br>
> is the reason of this behaviour? How does python deal with
default<br>
> values (i.e. when are they assigned/created)?<br><br>
This has been discussed before in this list, quite a few times<br>
<a href="http://mail.python.org/pipermail/python-list/2010-March/1239044.html" eudora="autourl">
http://mail.python.org/pipermail/python-list/2010-March/1239044.html</a>
<br><br>
A solution is to accept default value as None assign to [] by
checking<br>
for None inside the function<br><br>
def f(a=None):<br>
if a is None: a = []</blockquote><br>
See reference manual section 7.6 "Function definitions"
under the discussion subtitle "<b>Default parameter values are
evaluated when the function definition is executed.</b>
" <br>
<a href="http://docs.python.org/reference/compound_stmts.html#function-definitions" eudora="autourl">
http://docs.python.org/reference/compound_stmts.html#function-definitions<br>
<br>
</a>Yes, this is discussed in many places and many times, but <b>why
isn't it in the Python FAQ</b>? Amazing, yes?<br><br>
<blockquote type=cite class=cite cite="">-- <br>
Regards<br>
Shashank Singh<br>
<a href="http://rationalpie.wordpress.com/" eudora="autourl">
http://rationalpie.wordpress.com</a><br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" eudora="autourl">
http://mail.python.org/mailman/listinfo/python-list</a></blockquote>
</body>
</html>