Re: Modifying the default argument of function

2014-01-21 Thread Asaf Las
On Tuesday, January 21, 2014 9:46:16 PM UTC+2, Chris Angelico wrote: > On Wed, Jan 22, 2014 at 6:36 AM, Mû wrote: > > These were clear and quick answers to my problem. I did not think of this > > possibility: the default argument is created once, but accessible only by > > the function, therefore

Re: Modifying the default argument of function

2014-01-21 Thread Chris Angelico
On Wed, Jan 22, 2014 at 6:36 AM, Mû wrote: > These were clear and quick answers to my problem. I did not think of this > possibility: the default argument is created once, but accessible only by > the function, therefore is not a global variable, whereas it looks like if > it were at first glance.

Re: Modifying the default argument of function

2014-01-21 Thread
Le 21/01/2014 20:19, Chris Angelico a écrit : On Wed, Jan 22, 2014 at 6:11 AM, Mû wrote: The function acts as if there were a global variable x, but the call of x results in an error (undefined variable). I don't understand why the successive calls of f() don't return the same value: indeed, I

Re: Modifying the default argument of function

2014-01-21 Thread Chris Angelico
On Wed, Jan 22, 2014 at 6:11 AM, Mû wrote: > The function acts as if there were a global variable x, but the call of x > results in an error (undefined variable). I don't understand why the > successive calls of f() don't return the same value: indeed, I thought that > [2,3] was the default argume

Re: Modifying the default argument of function

2014-01-21 Thread emile
Function defs with mutable arguments hold a reference to the mutable container such that all invocations access the same changeable container. To get separate mutable default arguments, use: def f(x=None): if x is None: x=[2,3] Emile On 01/21/2014 11:11 AM, Mû wrote: Hi everybody, A frien

Re: Modifying the default argument of function

2014-01-21 Thread Steve Jones
On Tue, 21 Jan 2014 20:11:02 +0100 Mû wrote: > Hi everybody, > > A friend of mine asked me a question about the following code: > > [code] > def f(x=[2,3]): > x.append(1) > return x > > print(f()) > print(f()) > print(f()) > [/code] > > The results are [2, 3, 1], [2, 3, 1, 1] and [2

Modifying the default argument of function

2014-01-21 Thread
Hi everybody, A friend of mine asked me a question about the following code: [code] def f(x=[2,3]): x.append(1) return x print(f()) print(f()) print(f()) [/code] The results are [2, 3, 1], [2, 3, 1, 1] and [2, 3, 1, 1, 1]. The function acts as if there were a global variable x, but th