Everything is an object (Python3)

Everything is an object (Python3)

Python is a very approachable language has a reputation for being easy to read and write. Often it works just as you expect if you come to it from other languages. But you might suddenly encounter surprising behavior when you dig deeper.

The mechanism of Python are quite simple but sometimes their combined effects can be unexpected and intriguing. We must understand how it works internally to improve our quality when writing code.

We will clarify doubts about certain concepts that may be difficult to understand as immutable and mutable objects, names, values and assignments.

id and type

Each object has an identity, a type and a value. An identity object never changes once it has been created, we may think that it resembles a memory address in C.

id(object) -- function that returns the identity of an object

No alt text provided for this image

The "is" operator compares the identity of two objects

No alt text provided for this image

In the first example the identity is the same because since integeres are immutable, Python optimizes resources by making two names that refer to the same string value refer to the same object.

No alt text provided for this image

In the second example we can see that is False because the value is not the same, therefore the identity is different.

No alt text provided for this image

type() -- method returns class type of the argument(object) passed as parameter

No alt text provided for this image

mutable objects

mutable object can be changed after it's created.

Common mutable type in Python:

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


Immutable objects

A immutable object cannot be changed after creating it.

Common immutable type:

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


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

Mutable types can be changed in place, for instance:

We can see in the image below that d is a list of numbers and e has assigned d as value, if we change something of the list in e, that change also will be reflected in d because is the same object

No alt text provided for this image
No alt text provided for this image


ints are immutable for that reason when we assing values to a it points somewhere in memory. On the second line we assign the value of a into the name variable b and points to the same location but when we try to change the value of b a new object is created and b has a new value and a remains still.

No alt text provided for this image


No alt text provided for this image

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


No alt text provided for this image

We create the variable 'a' as a list object with some data

No alt text provided for this image

When we call the addList(list, value) function and executes

No alt text provided for this image

This behavior is very similar to what is seen in C when we pass a variable by reference but in Python the calls to a function by value or reference do not work that way.

Python uses a different call some say call by object or by assignment.

No alt text provided for this image
No alt text provided for this image

Above you see a string that is immutable. You can not change its content. Also, if we assign new content, a new object is created instead of the contents being modified, in this case this new object is only on the function scope.

References

Fluent Python: Clear, Concise, and Effective Programming - Luciano Ramalho 

Head First Python, 2nd Edition - Paul Barry

Immutable vs Mutable types - Stackoverflow

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

Sebastian Lopez Herrera的更多文章

社区洞察

其他会员也浏览了