Everything is an object… Mutable or Immutable.

Victor Zuluaga
3 min readSep 29, 2020

Intro.

This post we’ll do a deep dive in the concept “objects”, especially in python, we’ll learn a little bit more about data structure in Python like tuple(), list[], dict{}, and the main object like int(), str(), float(), etc.

Environment.

cases of example are written on the following system:

  • Ubuntu 14.04 LTS
  • Python 3.4
  • Interpreter’s Python

Prerequisites

In order to fully understand this article, you need to know:

  • Knowledge Basic handling interpreter’s Python
  • Python programming language

all data that you can manipulate in python are depicted in terms of objects, every object has at least three main features (Identity, type, value).

Let’s do a short description of these Objects.

Object Identity: Every object has an identity, this means is identified by unique object id. An object’s identity never changes once it has been created.

To understand better we talk through ID.

when we say identity, you may think of it as the object’s address in memory. supposing that we want to know the identity of the object we can use the built-in ‘id’.
id: Return the identity of an object. >>>help(id).

let’s see an example.

>>> a = 1 
>>> id(a)
140024684710016
>>>

if you are a programmer with a C background (like me) maybe you want to see that output in numbers hexadecimal for this you can use the built-in hex()

>>> a = 1 
>>> id(a)
140024684710016
>>> hex(id(a))
'0x7f5a09978880'
>>>

Object Type: is the data type to which the object belongs to. Python built-in function type() can be used to get the type of object.

>>> a = 1 
>>> id(a)
140024684710016
>>> hex(id(a))
'0x7f5a09978880'
>>> type(a)
<class 'int'>
>>>

when we use type() this returns us the class of the object in our example is ‘int’ this can be different data. remember in python there’re majorly two type’s of data which are numeric type (int, float, byte, bool, etc) and collection type (str, dict, set, list, tuple, etc).

Object Value: is content stored in the object. Objects whose value can change are said to be Mutable objects whose value is unchangeable once they are created are called Immutable objects.

How you can see above get into a new concept so far we haven’t seen (Mutable and Immutable).

Mutable objects: We say a data type is mutable when I can change its value without getting and error, Mutable means that you can create an assignment it values, and these values can change during the execution of the program.

But, wait… What are Mutable Objects and Why? Good question.

Let’s resolve here.

By convention python list all data type by hierarchy, mutable and immutable.
Datatype mutable.
* List
* Dict
* Set
So, Why it’s said that is Mutable. Parsing the following example.

>>> a = [1, 2, 3] 
>>> hex(id(a))
'0x7f3520bdfb00'
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> hex(id(a))
'0x7f3520bdfb00'
>>>

in the example, above we have a datatype list, we print its identity with built-in id(), then we use the method append to attach a new value at the same list, we print a and we see that it has a new value 4, and we print its identity again finally the most import to parse here is the identity(memory address) this no change is for this that we say lists are mutable we can change its value however we want and it’ll always keep the same identity.

Immutable objects: Immutable means cannot perform any changes.

Datatype Immutable are:
* int()
* float()
* complex()
* str()
* tuple()

this example we’ll do it with float

>>> x = 5.0 
>>> type(x)
<class 'float'>
>>> hex(id(x))
'0x7fec0806f2f0'
>>> x += 7.0
>>> x
12.0
>>> hex(id(x))
'0x7fec0814fad0'
>>>

Let’s parse the example above, here we have a datatype ‘float’, we assignment value 5.0, we print its identity with built-in id(), then we reassignment x and we add 7.0 the output is 12.0 this is the expected behavior but as we see chance its identity to a new. for this, we say that datatype float is immutable.

--

--