Title: Struggling with mutable/immutable default argument
Tags: python-3.x;parameter-passing;variable-assignment
Question: Coming from C++ and trying to learn Python I encountered the dreaded mutable default argument problem. 

```def func(L=[]):
    L.append(1)
    print(L)
```

I understood that the list gets longer and longer as a result of L being declared only once during function definition and not during execution and L pointing to mutable object. 

What I don't understand is how the solution to said problem works: 

```
def func(L = None):
    if L is None: 
        L=[]
    L.append(1)
    print(L)
```

What I don't understand is why, after the first time the function is called, L doesn't point to an object of type list with the value of [1].

Hear me out: 

```1:   L points to object of type None
2:    As L points to object of type None the if-statement evaluates to True
3:    L now points to a new object of type List with the value of []
4:    To the list to which L points a value of 1 is appended
5:     The value of the object pointed to by L is printed and shows [1]```

In the second iteration, as L is not going to be declared again, L should still point to an objevt of type List with the value of [1].

What am I missing? 

Is it because, L being immutable, Python creates a new variable L pointing to an empty list which is then destroyed after the functions returns and the garbage-collector recognizes the L of type None as the default parameter and, as a result, doesn't "kill" it? 

Thank you very much in advance for any type of help. 
Alessandro

Comment: Thank you very much. This video and the  resources provided in said video helped a lot. If I'm not mistaken the =[] evaluates only once and to the "address" of an empty list, which is then going to change. Is this right?

Comment: Perfect thank you. Little additional question: We do we have to check if L is None? Should it be always equaL to None?

Comment: Ops. Such a naive mistake.  Should have spotted it.

Comment: in your second example: the second time you call `func()` `L` gets reassigned to its default argument `None` (just as `L` would get assinged to `[1, 2, 3]` were you to call `func(L=[1, 2, 3])`). python has "call by assignment". this may help: https://nedbatchelder.com/text/names1.html .

Comment: yes, that is absolutely right! happy pythoning!

Comment: `L` is only `None` when you call `func()` (or `func(L=None)`). if you call `func(L=[1, 2, 3])` then it is not `None`.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:08/06/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<python-3.x><parameter-passing><variable-assignment>', 'Title': 'Struggling with mutable/immutable default argument', 'ViewCount': 35, 'id': 57374684, 'user_AboutMe': None, 'user_AccountId': 12454665.0, 'user_DisplayName': 'Alessandro Legnani', 'user_Reputation': 3.0, 'user_Views': 2.0, 'user_id': 9068279.0}}