Title: Passing mutable dictionary as default argument in Python
Tags: python;dictionary
Question: I am trying to implement the below code to reinitialize a new object. But it isn't happening
```class A(object):
    def __init__(self, arg):
        super(A, self).__init__()
        self.arg = arg

    def method(self):
        self.arg[1] += 1
        print(self.arg)

if __name__ == '__main__':
    a1 = A({1: 1, 2:2, 3:3})
    a1.method()
    a2 = A(a1.arg)
    a1.method()
    a3 = A(a1.arg)
    a1.method()


Expected Output: {1: 2, 2:2, 3:3}
                 {1: 2, 2:2, 3:3}
                 {1: 2, 2:2, 3:3}
                   
Code Output: {1: 2, 2: 2, 3: 3}
             {1: 3, 2: 2, 3: 3}
             {1: 4, 2: 2, 3: 3}
```
I found some links online where they explain about Python Mutable defaults. But couldn't find a solution to this problem.

Comment: So how can we achieve this behaviour?

Comment: Yes, this is not the actual usual case. Actually, I have a class Graph whose instance variable is a dictionary(adjacency list). I am computing the min cuts in this dictionary, so I am modifying the instances(in this case removing the dictionary keys). After finding the minimum cut, I have to run this process again. So I am creating another graph object and making use of the original graph object to do the min cut operation

Comment: After `a1.method()` you have no longer `{1: 1, 2:2, 3:3}` anywhere so you just can't if you use only one `arg`

Comment: You really want to reuse `a1.arg` ? Your usecase it not well defined or jsut seems to be a test, so if you want a good solution, provide a good usecase ;)


Here is another answer: Try changing the line
```self.arg = arg
```
to
```self.arg = arg.copy()
```
This way, each ```A``` object gets its own copy of the dictionary; they don't all have to share.

Comment for this answer: Can this be achieved without copy()

Here is another answer: you're only using ```method()``` on your ```a1``` so it's normal that it keeps updating ```self.arg[1]``` at each calls.

Maybe you would'd like to do this :
```a1 = A({1: 1, 2: 2, 3: 3})
a1.method()
a2 = A(a1.arg)
a2.method()
a3 = A(a1.arg)
a3.method()

outout : 
{1: 2, 2: 2, 3: 3}
{1: 3, 2: 2, 3: 3}
{1: 4, 2: 2, 3: 3}
```
but it does't work because the objects are &quot;sharing&quot; the same array. see Mutable vs Immutable Objects

So you can use ```.copy``` But even this will fail. Because when you're creating ```a2``` and ```a3```, you're passing ```{1: 2, 2:2, 3:3}``` in the argument because the initial arg have changed due to the use of ```method()``` on ```a1```
```a1 = A({1: 1, 2: 2, 3: 3})
a1.method()
a2 = A(a1.arg.copy())
a2.method()
a3 = A(a1.arg.copy())
a3.method()

outout : 
{1: 2, 2: 2, 3: 3}
{1: 3, 2: 2, 3: 3}
{1: 3, 2: 2, 3: 3}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:09/28/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><dictionary>', 'Title': 'Passing mutable dictionary as default argument in Python', 'ViewCount': 65, 'id': 64096580, 'user_AboutMe': None, 'user_AccountId': 19609878.0, 'user_DisplayName': 'Akshay Kekuda', 'user_Reputation': 1.0, 'user_Views': 2.0, 'user_id': 14352611.0}}