Python — Everything is Object
In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python everything is an object, which means every entity has some metadata (called attributes) and associated functionality (called methods).
Python id() and type() functions
In python, each object has an identity, type and value. The id function “id(object)” — returns a unique integer (identity) of a passed argument object — the integer is unique and constant throughout the object’s lifetime. For example;
In the above example, the print function returns the id of the object as 4426203184. Another object with a different lifetime, may have the same id value. The type function “type(object)” - either returns the type of the object or returns a new type object based on the arguments passed. For example;
The print(type(name)) function from the image above returns the type of the object passed as an argument to the type function as <class ‘str’> which implies the object is a string of characters.
Mutable Objects
Mutable objects are objects whose values can be changed in-place (changed without making a separate copy) after they have been created. There are basically 3 mutable data types in Python: List, Dictionary, and Set. For example, we can directly add, remove, or update the values of a list in Python, without creating a new list. Lets have an example;
In the above example, the object new_list is created an initialized with items 1, 2, 3 and 4. The next line of code appends a new item ‘5’ to new_list which is added to index 4 — new_list prints now reads [1, 2, 3, 4, 5]. Moving forward, the item at index 2 of new_list ‘3’ is changed/assigned a new value ‘5’ in place and new_list is updated to [1, 2, 5, 4, 5].
Get Kingsley Budu Boafo’s stories in your inbox
Join Medium for free to get updates from this writer.
Immutable Objects
With immutable objects, we can not change the the contents of an immutable data type in Python.In simple words, the value assigned to a variable cannot be changed for the immutable data types. For example, String is an immutable data type in Python. We cannot change its content, otherwise, we may fall into a TypeError. Even if we assign any new content to immutable objects, then a new object is created (instead of the original being modified).
Remember the variable ‘name’ we created at the start? If we try to change the value of the character at index 4 returns an error. Immutable data types in python include: Numbers, Strings, and Tuples.
Mutable Vs. Immutable Objects — Why Does it Matter?
Python treats mutable and immutable objects differently. Here are some differences between mutable and immutable objects.
- Mutable objects are not considered as thread-safe in nature while immutable objects are regarded as thread-safe in nature.
- Mutable objects are slower to access, as compared to immutable objects while Immutable objects are faster to access when compared to mutable objects.
- Mutable objects are useful when we need to change the size or contents of our object. On the other hand, immutable objects are best suitable when we are sure that we don’t need to change them at any point in time.
- Changing mutable objects is a cheaper operation in terms of space and time while changing immutable objects is an expensive operation since it involves creating a new copy for any changes made.
Hope you found this article helpful!






