What are mutable and immutable objects in Python3?

dude!>>> name = "guido">>> name[0] = 'G'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignmentFrom above we can see that this is not supported by an immutable object and we get a TypeError that is a reminder that ‘str’ objects are immutable and do not support item assignment.

Pay Attention HereThis is a huge deal because Python treats mutable and immutable objects differently.

If you don’t keep track of the qualities of both, it might lead you to unexpected bugs.

Take the following example:>>> account_balance = 1000000>>> balance_to_transfer = account_balance>>> account_balance = 2000000>>> balance_to_transfer1000000Obviously banking systems aren’t this simple but you get the point… if you don’t know the types you are working with you could make some serious errors in your applications that could lead to being costly.

To make things more confusing take this example for instance.

Note: remember that lists are mutable objects whereas integers (above example) are not.

Let’s say we have some software system in a grocery store that keeps track of two distinct lists of SKUs new_items and perished_items:>>> new_items = ['sku0001', 'sku0002', 'sku0003']>>> perished_items = ['sku1000', 'sku2000', 'sku3000']>>> perished_items = new_items>>> perished_items>>> ['sku0001', 'sku0002', 'sku0003']>>> perished_items[0] = 'sku1111'>>> new_items['sku1111', 'sku0002', 'sku0003']You can go here for the step-by-step preview: https://goo.

gl/CweUTBbut essentially here is a rundown of what just happened:results of misunderstanding your types from PythonTutor.

comWe set the list variable perished_items equal to new_items at some point in the code (this would be a bug unless it was intentional for some reason to create a copy and use it somewhere but the programmer forgot that it was declared or something)Next, when we modified the first element in our perished_items list (which could easily happen by the way from some nefarious function in the program somewhere), it also modified our variable in new_items because the perished_items had created a clone of the original list, new_items.

This unexpected result has to do with type list’s mutability.

Let’s walk through what is happening step by step in the code.

In the bank example above:>>> account_balance = 1000000>>> balance_to_transfer = account_balance>>> account_balance = 2000000>>> balance_to_transfer1000000When account_balance = 1000000 runs, the object 1000000is created, and the variable name account_balance is associated with this object from the global frame.

Next, balance_to_transfer = account_balance sets a pointer from balance_to_transfer to the same 1000000 object that has a pointer from account_balance pointing at it.

In C this is just normal behavior but in Python it is known as aliasingUpon running account_balance = 2000000 the integer object 2000000 is created, and that account_balance pointer will point at that same value.

The namespace balance_to_transfer is still associated with that 1000000 value and remains unchanged when account_balance is reassigned, so the variable balance_to_transfer returns the value 1000000 .

As you can see if this were a real banking system you’d have some pretty angry high roller clients and bugs in this software system could mean losing a very important userBut what about that grocery store software example before?new_items = [‘sku0001’, ‘sku0002’, ‘sku0003’] which is a list type object is created and new_items points to this mutable list object.

perished_items = new_items sends a pointer from perished_items to the same list object [‘sku0001’, ‘sku0002’, ‘sku0003’]If you click on the link in the first example of this grocery store software system you’d see this visually but to show logical proof here is a simple example you can run:>>> id(new_items) == id(perished_items)True>>> new_items is perished_itemsTrueis tests for object identity which returns True if they are in fact the same object== is different though because it is testing the value of the two objects!Because these two names refer to the same list, when we modify one of them perished_items[0] = sku1111, the change is made to the single list in memory but reflected in both variable names.

Editors note: I hope that you enjoyed my essay on this topic!.If you’d like to read more of my work please follow me.

If you want to reach out on social media Twitter is your best bet!.-@connorbrer.. More details

Leave a Reply