Mutable, Immutable… everything is object!

Mike Mbea
4 min readJan 13, 2021
I wish my life was an object

Python is a wonderful language, but coming from C, one might be a little confused as to what objects actually do. What is or isn’t an object? There are object types, object ids, mutable objects, immutable objects — it can be overwhelming. So, here’s a quick rundown of some of the characteristics of Python’s objects.

An object’s type is the class it belongs to. For instance, integers are of object type int, strings str, and so on. Python also allows for the creation of classes by the programmer. To access the type of an object, wecan use the type() function, or to check whether the object is an instance of a given class, use isinstance([object], [type]). An object’s id, however, is slightly different. Try checking any object’s id using id(). A number will be returned, which is the memory address that object is stored at.

Mutable versus Immutable objects

one looks better than the other one

If we look at a list, say [1, 2, 3], we can assign that to two variables separately:

We can see that even though the lists have the same content, they are two separate objects. A simple a is b test will return False, because these two objects can be acted upon separately. This is known as being mutable. If we append to one of the lists, we can see that they really are separate.

In order to make two variables point to the same mutable object, we would have to assign b = a instead of assigning it to the same list.

As we can see, variables that are mutable can be modified after creation. Immutable variables, however, differ in that they cannot be changed, with one workaround. We can create a new, different instance of the object, and reassign the variable to point to the new instance:

While the variable a still exists, it is being reassigned to a new str object, as is evidenced by the differing ids.

Something to be aware of is that for shorter strings, which are of an immutable type, is that they will be stored as a single object in memory. For instance:

However, for longer strings, this may not be the case. Always test by using a is b before you determine whether two objects are actually the same.

Passing variables to functions

Trying to modify different variables by way of a function has different implications for mutable versus immutable types. As an example:

Passing an immutable type to a function with no return will not modify the variable. b has been defined within the scope of our main function, but when it is passed into switch, it is only modified as a within switch's scope. When we leave the function switch, a is no longer in the namespace, so b points to the same string as before.

Mutable variables are handled in the opposite way. If we try the same concept with a list, we get this result.

Since the type list is mutable, the object itself is passed into the function, so regardless of name assignment, the object is changed.

By now, you hopefully know a little more about how Python handles different objects. If you like what you’ve read, follow me on Twitter.

--

--