Re: [Tutor] Surprising behaviour of optional argument

2014-07-17 Thread Alan Gauld
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 :-

Re: [Tutor] Surprising behaviour of optional argument

2014-07-17 Thread Jose Amoreira
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

Re: [Tutor] Surprising behaviour of optional argument

2014-07-17 Thread Danny Yoo
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 >

[Tutor] Surprising behaviour of optional argument

2014-07-17 Thread Jose Amoreira
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(