Objects in Python

Introduction

Since Python is an object-oriented programming language, it means that every data is stored and treated as an Object. This happens with the use of Classes.

Basically Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

So a basic Class(template) to represent cars could be:

```class car: 
 def __init__(self, make = 0, model = 0, year = 0):
 self.__make = make
 self.__model = model
 self.__year = year

Mycar = car(Honda, Civic, 2002)

```

So now we have a class “car” to store infornation about the car like Type, Make, Model and Year. After creating the class we defined an object “Mycar” and made it a 2002 Honda Civic. The same applies to all standart variables types.


Id & Type

Now that we agreed that everything is object then we can agree that everything should have an identity or memory address for C users. The identity is a unique number for every object during its lifetime. We can retrive this number with the id() function like this:

```
>>> a = 11
>>> id(a)
494672865096

```

You can also find the type of the object using the function type() which basically indicates the class of the object that is being passed.

```

>>> a = 11
>>> type(a)
<class 'int’>

```

Remember this can be aplied to EVERYTHING in python.


Mutable and Immutable objects

Untill now we learned that all data in python is represented as objects and all objects have a unique identity. Objects now are seperated in Mutable and Immutable by the ability to change their content value without changing their Identity. Mutable objects are lists, sets and dictionaries and Immutable is basically everything else.

To better understand immutable objects check in the following example how the id changes.

```

>>> a = 5
>>> id(a)
494672864904
>>> b = a
>>> id(b)
494672864904
>>> c = b
>>> id(c)
494672864904
>>> a += 2
>>> id(a)
494672864968
>>> id(b)
494672864904
>>>

```

Now check the mutable example:

```

>>> a = [1,2,3]
>>> id(a)
494662539848
>>> b = a
>>> id(b)
494662539848
>>> a += [3]
>>> id(a)
494662539848
>>> print(a)
[1, 2, 3, 3]
>>> print(b)
[1, 2, 3, 3]
>>>

```

You can notice that adding a value in the list didn’t change it’s identity but adding some value to an int changed its identity therefore it changed the object it’s refering to.


Why it matters if an object is mutable or immutable

So after knowing all the above it raises the question “why does it really matter?”

The answer could be it doesn’t, but if you think about for a second and wonder how do we process most of the data (using functions) and that unlike C , in python everything is pointer. Therefore when you passing an argument to a function you are passing the “pointer” or “alias” to it so if its mutable the function is going to change it’s value if it’s not we must be able to handle that and return a new object with the new value.

If you found this helpful give it a clap!

Thank you!!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.