Python3: Mutable, Immutable… everything is object

Manuel Enrique Figueroa
5 min readJan 13, 2021

Introduction:

Python is a Object oriented programming language which essentially means that everything in it is based around Classes and Objects. Almost everything you use in python is going to be an object centered around a class. One of the most common things are variable data types. For example when you declare a Tuple in python it is just declaring a class object from a Tuple class. Variable Types in python are separated into two categories Mutable and Immutable which will be discussed along with some important tools for working with objects in python.

Mutable objects:

Mutable objects are exactly what there names imply. They are object types that can be edited after they are declared in python. Some mutable examples would be Lists, Sets, or Dictionary. Essentially when you create a variable of a certain Type is assigns that object with a specific object Id. When using a mutable type you can change the values associated with the object without altering this Id. This allows you to change values in place.

Immutable objects:

Immutable objects are the opposite of Mutable objects. They are object types that cannot be edited after they are declared. Some examples of these are Numbers(int, float), strings, and tuples. When you try to change these types you will that specific object id will be changed fundamentally.

This is a useful chart to show what is Mutable and Immutable in python

Id and Type:

To further our understanding of how Immutable and Mutable types work there are a couple of tools we can use. These tools are called the id() and type() functions and we will use them in a couple of examples.

Printing Id:

Here we use the id() function to print the id of an integer called a

Printing Type:

Here we use the type() function to print the type of object of an integer called b

These are useful for keeping track of variables type and whether or not they are the same variables in memory especially

How does python treat Immutable and Mutable types?

Immutable:

Immutable types are great if what you have stored in them needs to stay the same. When using Immutable types you have to keep in mind that every time you “modify” one a copy is created for example.

Here we are creating a Immutable string named a. When we decide to append to this string we create a brand new object with a new object id. Now lets try and change a character from our string.

As you can see we get a TypeError that states that our class str does not support item assignment.

Another simple and most common example of a immutable type assignment is using integers…

Mutable Types:

Mutable types are less cumbersome then immutable types and allow for more dynamic use of your memory. Here are some examples of using lists.

As you can see above even after mutating our lists the Id stays the same.

Telling the difference:

There is a couple of things you can do in python to tell whether or not a data type is equal in identity or value:

<object> is <object:

The <is> operator will return true or false if based on if the Id of out object is the same as the other. This is useful so we can tell if a object has the same Id or is different. A good example is telling whether a list is a copy or alias of the original

As you can see above when creating nice we create a alias instead of a copy int turn both object names having the same Id. You can tell if a list is a copy or a alias by using <is>

You can avoid creating an alias by cloning the list using the list() object as follows

As you can see nice is know a clone of cool and if you edit cool or nice the other will not be affected as if it where an alias.

Mutable and Immutable in functions:

Now we have to understand the difference between Mutable and Immutable objects when passed to functions. Here is an example of a immutable type being passed to a function.

As you can see our immutable integer was passed to the function but was not altered in outside of the functions scope. However if we were to pass a mutable list to a function the result is completely different.

As you can see our list was edited within out given function which is really handy but also detrimental if used improperly. If you intend on keeping the state of the original list you will need to clone it and return the new list within the function…

Tuples:

Tuples are immutable types but form a bridge between the two worlds of mutable and immutable. Tuples are able to hold both immutable types such as integers, floats, and strings but also mutable types of list and dictionarys. The tuple itself cannot be modified without creating a copy but its contents if they are mutable can be edited with the tuple as if they were normally stored.

For example here we have a tuple with two lists and we try to edit both the tuple and the list…

All the id’s for the lists and the tuple are different
As you can see here we appended 0 to the first list in the tuple yet the id stayed the same
And we see and error when we try to edit the tuple values

This is very handy to know when working with tuples…

--

--

Manuel Enrique Figueroa
0 Followers

I am a current student at Holberton school of software engineering aspiring to have a career in full stack software engineering.