On Sun, Jun 26, 2011 at 11:28 AM, Marc Aymerich <glicer...@gmail.com> wrote:
> Hi,
> I'm trying to define a function that has an optional parameter which
> should be an empty list whenever it isn't given. However, it takes as
> value the same value as the last time the function was executed. What
> is the reason of this behaviour? How does python deal with default
> values (i.e. when are they assigned/created)?
>
> Thanks :)
>

So the thing about Python is that you don't actually declare
functions. You create them. def is an executable statement that
creates a function object. Default arguments are part of the function
object, so they get evaluated when the function is created.

>>> foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> def foo(a = []) :
...     a.append(1)
...
>>> foo.func_defaults
([],)
>>> foo()
>>> foo.func_defaults
([1],)
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to