Mutable, Immutable... In Python everything is object!

Mutable, Immutable... In Python everything is object!

Introduction

Hi, if you are here I can assume that you are interested in learning about python, if so, you are in the right place.  I will assume that so far you are starting, so the first thing you should know is that, in Python each and every one of the things are objects.

No hay texto alternativo para esta imagen


In this example we create a class called StrangerThings which has an attribute (eleven) and a method (OneLiner)

The attributes are those that define the object, the methods are the functions with which we can modify our object, and the class is our mold that creates the object.



Id and type

No hay texto alternativo para esta imagen

The id function receives a unique parameter, and we use it to know the unique identifier of each object, making a parallel with c is like the memory addresses used in that language. As you can see, in the example each estring has a different id, that is because each is a different object


No hay texto alternativo para esta imagen


And the type function essentially allows us to know to which class an object belongs. In example 'a' is an integer and belongs to the int class, 'b' is a float and belongs to the float class and yes, you guessed, 'c' is a string and belongs to the string class


Operator "==" vs "is"

In python we have two types of operators that allow us to evaluate the value and the Id of our objects, to evaluate the value we use the comparator "==" which will evaluate if two objects have the same value, if so, True will return and otherwise return False and the operator is evaluating whether two objects have the same Id if so, it will return True otherwise it will return False.

No hay texto alternativo para esta imagen

As you can see, a and b have the same value, so the operator "==" returns True, but they are not the same object, so the "Is" operator returns False, while c, it is simply what we would call a pointer a "a" which makes the same object in the background a and c, so the comparator "is" returns True.

Mutable and inmutable objects

Mutable objects are those that once created we can modify through their attributes, while immutable objects once created do not allow us to modify them.

No hay texto alternativo para esta imagen

List

The lists being mutable objects I can modify them without having to create a new object.

No hay texto alternativo para esta imagen

As you can see with the .append method you can add a new member to the list, but although the object was modified its Id is the same, which means that it is a mutable object.

Tuple

In contrast, tuples are immutable objects which means that once created I cannot modify them unless I create a copy of the object (which will cause its Id to change). As you can see, to modify the tuple first I need to change it to a list (which if it is an immutable object) and then go back to a tuple assigned again to "a" a value, but this has created me a new object with an id different from the previous one, this is what is known as an immutable object.

No hay texto alternativo para esta imagen

References

https://hackernoon.com/python-objects-and-mutability-397f7de38bb5

https://stackoverflow.com/questions/8056130/immutable-vs-mutable-types

https://stackoverflow.com/questions/15008380/double-equals-vs-is-in-python

https://www.tutorialspoint.com/How-we-can-update-a-Python-tuple-element-value?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了