Carlos Alvarez
3 min readOct 9, 2019

Python3: Mutable, Immutable… everything is object!

In the next few lines, I will explore what it means exactly because everything is an object in python. This short guide is intended to provide a minimum education on how objects are used to represent values ​​in Python, a knowledge that is essential for learning object-oriented programming. In today’s post, I will start with a basic introduction to the objects that are in Python.

Object Type

Un object represents a value a variable can refer to. For instance, in the following example, the variable x refers to an object representing the value 1.

>>> x = 1

All objects in Python are represented by a generalized structure used to describe and access a particular value’s information and methods. To determine the type of an object, you can use the built-in method of the same name, type(). Building on the above example, the type of x, which refers to the value 1, is the numeric type int:

>>> x = 2
>>> type(x)
<class 'int'>

If x referred to the string "hello", it would be an instance of the sequence type str:

>>> x = "hello"
>>> type(x)
<class 'str'>

OBJECT IDENTITY

There is more to objects than just their type — for something to be an object in the first place, it has to exist somewhere in memory. To view the memory address of a particular object, you can use the built-in method id().

>>> x = 2
>>> id(x)
10105088

The id() method returns the identity of an object, an integer memory address reading memory addresses in hexadecimal format — to achieve this, you can combine the id() and hex() methods.

>>> x = 2
>>> hex(id(x))
'0x9a3100'

addresses may vary from machine to machine.

OBJECT COMPARISON WITH is & ==

We can compare the memory locations of any two objects in Python with the is operatoris returns True if two variables have the same identity (example: they refer to the same object), and False otherwise.

>>> x = 56
>>> y = 65
>>> x is y
False

In the above example, x refers to the memory location of an int object 56, while y refers to the separate location of int object 65, thus, the two variables have different identities, and x is y is False. In more Pythonic terms, x is not y:

>>> x is not y
True

Note that the value of an assignment expression is calculated before a new object is attached to a variable. The above is equivalent to using the value of x in an expression assignment for y:

>>> x = 89
>>> y = x + 9
>>> x is y
False

OBJECT INSTANTIATION | IMMUTABLE OBJECTS

When trying to instantiate a new object in memory, Python doesn’t just do it involuntarily. Rather, for efficiency, objects are created after receiving instructions to instantiate an object, Python first verifies its existing memory. If an instance of that object already exists, instead of allocating memory to create a new one, it will use the pre-existing value.
For example, the following example that compares objects with equivalent identities:

>>> x = 56
>>> y = 56
>>> x is y
True

OBJECT INSTANTIATION — MUTABLE OBJECTS

Python tries to avoid instantiation of multiple immutable objects, since the value of an immutable object is directly related to its identity. However, instantiation of mutable objects does not take duplicates into account.
Mutability can change as changing: the characteristics of a mutable object may change over time, while retaining the identity of the original object.
An analogy is animals. Its characteristics indicated with time: your hair will be longer tomorrow than it is now, but at the end of the day, your identity will remain unchanged, being the same animal.

No responses yet