Mutable objects vs Python functions.
Tags: function, programming, python, mutableobjects
Published: 2018-03-27 02:04:02

---
title: Mutable objects vs Python functions.
published: true
tags: function,programming,python,mutable-objects
canonical_url: https://medium.com/@adekoder/mutable-objects-vs-python-functions-e0bb2dea5945
---

We know every thing in python is an object and can be classed into mutable and immutable, where mutable object are object whose state can change after construction while the latter are object whose state cannot be change after construction.

![](https://cdn-images-1.medium.com/max/1024/1*UB6ooKAQT8cJH5BapJFsHw.png)<figcaption>code snippet made pretty by <a href="https://carbon.now.sh">https://carbon.now.sh</a></figcaption>

Both Dictionary and List in python are mutable, therefore caution should be taken when passed as an argument to functions because they are passed by reference and not by value, meaning that when passed to a function as an argument the object is referenced and a copy of the object is not created which might cause a bug depending on what you’re trying to achieve with your code.

Here is a code snippet to understand what is happening when you pass a mutable object to a function.

![](https://cdn-images-1.medium.com/max/1024/1*roM1ggVVs_LJvPXT3wFljg.png)<figcaption>code snippet made pretty by <a href="https://carbon.now.sh">https://carbon.now.sh</a></figcaption>

In this code snippet above, i have a variable **“leader\_board”** that references a dictionary object which hold a data of leaderboard score, also there is a function which add a new member to my leader board, the function **“add\_new\_member”** accepts a dictionary object of the leaderBoard, the name of the new member and the score, when this function is called and passed in the **“leader\_board”** object, the name of the new member **(‘janet’)** and the score **(12).**

> **leader\_board[name] = score**

This state above within the function add a new key:value pair data to the leader board object in memory.

when the function **“add\_new\_member”** is called it reference the leader\_board object in memory and change the value in-place, when the leader\_board is printed within the function we get.

> {‘james’:23, ‘doe’:20, ‘mat’: 15, ‘janet‘: 12}

Also when the leader\_board object is print out side the function we get the same result. To confirm this let run an object test by updating the code to return the leader\_board from the function and use the **“is”** operator to do object test.

![](https://cdn-images-1.medium.com/max/1024/1*5FLRpuwucOasdHDy3Z9ICg.png)<figcaption>code snippet made pretty by <a href="https://carbon.now.sh">https://carbon.now.sh</a></figcaption>

> print(result is leader\_board)

This above statement will return True because the **“is”** operator is checking if the two operands i.e result and leader\_board are referencing the same object and are of equal value equal.

I also added a new block of code below in the codebase to show that the **“is”** operator actually does what it does the **“print( leader\_board is leader\_board\_2)”** will return False because the two are different object in the memory, even if they have the same data they must reference the same object.

{% gist https://gist.github.com/adekoder/ab8c74e978692e3d5c02c0c33fb81275 %}

### Overriding the universe

Mutable object being passed by reference to a function might be useful in must cases but what if that not how i want my code to behave, what if i want my code to accept a mutable object and return a new one without affecting the result of the one passed to it, the solution is to use the **“copy()”** method provided by mutable objects, the copy function return a shallow copy of the object and that can be assigned to another variable.

![](https://cdn-images-1.medium.com/max/1024/1*XT7UVg0R5zh9QhwvhUW-4A.png)<figcaption>code snippet made pretty by <a href="https://carbon.now.sh">https://carbon.now.sh</a></figcaption>

With the new updated codebase above now using **“copy”** method with the mutable object, return a shallow copy of the data and create a new object in memory , and you can see that our **“result”** object is different from the **“leader\_board”** object, when we do an object test **“print(result is leader\_board)”** we get False to show that they are not the same object in memory.

> Note: the whole story above apply to List object as well, it also mutable.

### Conclusion

So depending on what you’re trying to achieve with you code know when to really on mutable object being passed by reference and when to override the behaviour else 😢 🐛 🐛 😢.

Don’t forget to share this with your fellow pythonistas you might save a soul.

Thank you.