HOW PYTHON WORKS WITH DIFFERENT KINDS OF OBJECTS
Published: 2023-10-04 03:53:02

**Introduction**

_“In Python, everything is an object.”_

Python is an object-oriented programming language, and an object is a software unit that combines data and methods. Objects such as variables, functions, lists, tuples, dictionaries, sets, etc., are members of a specific class. Classes are the user-defined building blocks that allow us to create “objects.” An object that belongs to a class is an instance. Classes and objects are two main aspects of object-oriented programming (OOP). Python classes offer all the standard features of object-oriented programming, including the ability for multiple base classes under the class inheritance mechanism. Objects may contain any number and type of data.

**Scope**

In this article,

- We will touch on mutable and immutable objects.
- Briefly discuss Id and id type
- Then, why it does matter, and how differently does Python treat mutable and immutable objects
- Then, how arguments are passed to functions, and what it implies for mutable and immutable objects

Let’s dive in…..



**Mutable Objects**

_“Every object has an identity, a type, and a value.”_

The value of some objects can change. Objects whose values can change are said to be mutable. An object’s type determines how mutable it is. An example of a mutable object is a list. The elements of a list can be modified, the values can be replaced, and the order of elements can be changed.


![Objects](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5mp1icy53pqajjc4wz3.jpg)

Other examples of mutable objects are sets and dictionaries.


**Immutable Objects**

On the other hand, not all values of an object can be changed. Objects whose values cannot be changed are said to be immutable. An example of an immutable object is a tuple.


![Object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlzkeuyk3nt3tyx50vx0.jpg)

Other examples of immutable objects are floats, bool, strings, tuples(however, they can be mutable if they contain lists), and frozen sets.



**Id and Type**

In Python, each object has a distinct identity. When an object is created, an id is given to it. The object’s memory address, or id, is unique each time the program is run. (Except for some objects, like integers between -5 and 256, with a fixed, unique id). The built-in id() function returns the unique id for the specified object.

![Object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j80ipi8hb2h7iyldxmt1.jpg)

The equality operator (==) can be used with this function to check whether two variables are pointing at the same thing.

![Object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3juju47ddmh84dm9lvd.jpg)

Moreover, we apply Python’s built-in type() function to determine what data type is held in a variable. Based on the arguments it receives, the built-in Python function type() can print the type of function parameters. The type() function accepts either a single argument or three arguments. If only one argument is supplied, type(object) returns the type of the supplied object. It, however, returns a new type object if three arguments, type(name, bases, dict), are supplied.


![Object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyudqzzsovxph2ced2rz.jpg)


**Why does it matter and how differently does Python treat mutable and immutable objects?**

Mutable and immutable objects are handled differently in Python.
Compared to mutable items, immutable ones are easier to obtain.
When you need to adjust the size of an object, for example, list, dict, etc., mutable objects are excellent to use. When you need to ensure that the object you created will never change, you use immutable objects. Immutable objects are inherently difficult to alter. Mutable things are relatively inexpensive to modify.


**How arguments are passed to functions, and what does that imply for mutable and immutable objects?**

A mutable object can modify the original variable when referenced in a function. Therefore, the original variable must be copied to another variable to prevent this. Because their value cannot be altered, immutable objects can be called by reference.