Python3: Mutable, Immutable... everything is an object!

Python3: Mutable, Immutable... everything is an object!

Introduction.

If you are familiar with Python, you probably know that everything is an object, but don't worry, for those of you that are not familiar with this programming language the concept of an object is simple. Let us think about a mold that we use to bake cakes, so everything goes into the mold and when we bake it, we obtain a cake that has attributes and capabilities attributes like its color or flavor and capabilities like it can be eaten. So, in a nutshell, the mold represents the class that creates objects like the cake, that same principle applies to everything in python for example when we define an int variable a = 5, we could say that the object '5' belongs to the class 'int'.

once we have clear what an object is in python we can go ahead and define some important concepts to understand better whether an object is mutable or not.

Id and type.

Python id() function returns an identity of an object. This is an integer that is guaranteed to be unique. This function takes an argument an object and returns a unique integer number that represents identity. Two objects with non-overlapping lifetimes may have the same id() value. A simple example to illustrate this is shown below:

>>> a = 5
>>> b = [1, 2, 3 , 4]
>>> id(a)
9789120
>>> id(b)
1403815286342405        

On the other hand, we have the type() function that is used to get the type of an object. here is an example of how it works:

>>> x = 10
>>> type(x)
<class 'int'>
>>> s = 'abc'
>>> type(s)
<class 'str'>        

with these important concepts clear we can know understand how mutability works on objects.

mutable objects.

Mutable objects are used to represent values that change over time. some examples of mutable objects in python are the next ones:

Common mutable type:

  1. mutable sequences:?list(),?bytearray()
  2. set type:?set()
  3. mapping type:?dict()
  4. classes, class instances
  5. among others...

immutable objects.

contrary to the mutables tho inmmutbles ones can't change over time without changing it's identity also (id()). Some examples of this type of objects are the following:

Common immutable type:

  1. numbers:?int(),?float(),?complex()
  2. immutable sequences:?str(),?tuple(),?frozenset(),?bytes()

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

Python handles mutable and immutable objects differently. Immutables are faster to access than mutable objects. Also, immutable objects are fundamentally expensive to “change”, because doing so involves creating a copy. Changing mutable objects is cheap.

Mutable objects are very good when you need to resize the object, i.e. it will be dynamically modified

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

The way that the Python compiler handles function arguments has to do with whether the objects in the function arguments are mutable or not immutable.

If a mutable object is called by reference in a function, the original variable may be changed. If you want to avoid changing the original variable, you need to copy it to another variable.

When immutable objects are called by reference in a function, their value cannot be changed.






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

社区洞察

其他会员也浏览了