Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The value of an empty list in function parameter, example here [duplicate]

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

def f(a, L=[]):
    L.append(a)
    return L

print(f(1, [1, 2]))
print(f(1))
print(f(2))
print(f(3))

I wonder why the other f(1), f(2), f(3) has not append to the first f(1, [1,2]). I guess the result should be :

[1, 2, 1]
[1, 2, 1, 1]
[1, 2, 1, 1, 2]
[1, 2, 1, 1, 2, 3]

But the result is not this. I do not know why.

like image 969
stardiviner Avatar asked Mar 27 '12 09:03

stardiviner


1 Answers

There are two different issues (better to called concepts) fused into one problem statement.

The first one is related to the SO Question as pointed by agh. The thread gives a detailed explanation and it would make no sense in explaining it again except for the sake of this thread I can just take the privilege to say that functions are first class objects and the parameters and their default values are bounded during declaration. So the parameters acts much like static parameters of a function (if something can be made possible in other languages which do not support First Class Function Objects.

The Second issue is to what List Object the parameter L is bound to. When you are passing a parameter, the List parameter passed is what L is bound to. When called without any parameters its more like bonding with a different list (the one mentioned as the default parameter) which off-course would be different from what was passed in the first call. To make the case more prominent, just change your function as follow and run the samples.

>>> def f(a, L=[]):
        L.append(a)
        print id(L)
        return L

>>> print(f(1, [1, 2]))
56512064
[1, 2, 1]
>>> print(f(1))
51251080
[1]
>>> print(f(2))
51251080
[1, 2]
>>> print(f(3))
51251080
[1, 2, 3]
>>> 

As you can see, the first call prints a different id of the parameter L contrasting to the subsequent calls. So if the Lists are different so would be the behavior and where the value is getting appended. Hopefully now it should make sense

like image 79
Abhijit Avatar answered Oct 02 '22 21:10

Abhijit