Python [im] + [mutable] data

Colton Walker
3 min readJan 27, 2017

With the Python language, understanding that “everything is an object”, and how the object “type” works is one of the keys becoming a good python coder. Each object has a type(list, string, tuple, etc) also referred to as class. each type falls into one of two categories mutable and immutable or put in another way, variable values that stay locked and cant be changed and variable values that are unlocked and can be manipulated. The types can also be nested in one another and can get tricky pretty quickly.

Lets go over some of the basics of understanding the types and how they work. First, there are built-in functions that allow the return of some key information. Using the id() function will return a unique memory address of a given object. The type() built-in function will return… well the type!

>>> a = 924
>>> type(a)
<class 'int'>
>>> id(a)
234652

Now lets check out the different data types and see what category they fall under. (Data Type Table 1.0)

Data Type Table 1.0

So what does immutable truly mean? If you’re like me, you’ll need some visuals.

>>> a = (8, 1, 5)
>>> type(a)
<class 'tuple'>
>>> id(a)
234652000
#lets try and change 'a' which is immutable
>>> a[2] = 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

What happened? We attempted to change the variable “a” which holds a value type of a tuple. A Tuple cant be changed, which is why a type error message was raised. This is handy when working with functions. Immutable a very consistent because of their restraints. You know by passing a immutable type into a function, you will receive the data back in the same condition you sent it off in.

Lets now take a look at a type we can mutate or change… a list!

>>> a = [8, 1, 5]
>>> type(a)
<class 'list'>
>>> id(a)
234652000518
>>> b = a
>>> type(b)
<class 'list'>
>>> id(b)
234652000518
>>>b is a
True
>>> a[2] = 815
>>> print(a)
[8, 1, 815]
>>>print(b)
[8, 1, 815]

As you can see the variable “a” was able to change. We made a variable “b” prior to variable a’s change so we know the actual variable was changed vs a copy of it being made; which technically happens in the background without you knowing. Same as if you were to add two list together. The computer will automatically create a new list in the background and gives you, the programmer, a new list.

>>> a = [1, 2, 3]
>>>id(a)
139808250112408
>>> b = [4, 5, 6]
>>> id(b)
139808250054920
>>> a = a + b
>>> print(a)
[1, 2, 3, 4, 5, 6]
>>> id(a)
139808250127712

Did you notice the two memory addresses “a” has had? Again, python is doing what it does best. It does the work for us! Interestingly, if you use a tuple that has a list, the list can be changed.

>>> a = [1, 2]
>>> b = (1, a)
>>> print(b)
(1, [1, 2])
>>> a[1] = 0
>>> print b
(1, [1, 0])
>>> id(a)
139808250054920
>>> id(b)
139808311058232
>>> type(a)
<type 'list'>
>>> type(b)
<type 'tuple'>

Like I said, it can quickly get out of hand if you wish to mix the two data types, so keep an eye on that if you like to live life on the edge.

I hope this article has shed light on the importance of object types. This is considered to be a must in your foundation as a python programmer and leads to powerful and reliable programing.

--

--