On 17/07/14 23:00, Jose Amoreira wrote:
Aha, the object that stores the default value for the optional argument
is created at the time of the execution of the function definition and
mutable objects do mutate, so that's that. It makes a lot of sense.
Yes, think of it as a feature, not a bug :-
Hi
On 07/17/2014 06:34 PM, Danny Yoo wrote:
Yeah; the default value is not reevaluated between calls. It's a common
gotcha. Here are a few links to read more:
http://effbot.org/zone/default-values.htm
http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments
Good
On Jul 17, 2014 10:22 AM, "Jose Amoreira" wrote:
>
> Hello
> I stumbled on something I found surprising. I'm using standard python
(cpython, I guess) 2.7 on openSuse 13.1.
>
> Consider the function
>
> In [2]: def f(x,y=[]):
>...: print y
>...: y.append(x)
>...: return x
>
Hello
I stumbled on something I found surprising. I'm using standard python
(cpython, I guess) 2.7 on openSuse 13.1.
Consider the function
In [2]: def f(x,y=[]):
...: print y
...: y.append(x)
...: return x
This is the output of repeated calls to this function:
In [3]: f(