On 6/26/2011 2:28 PM, Marc Aymerich 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)?

Our fine Language Reference. Compound Statements chapter, Function definitions section, says in bold type: "Default parameter values are evaluated when the function definition is executed. ". I presume the tutorial says this somewhere too. Read both, along with the first 5 chanpter of the Library reference.

If you want code executed when you call the function, put it in the body that is executed when you call the function

def f(lst = None):
  if lst is None:
    lst = []
    ...

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to