Everything is an object (Python3)
Sebastian Lopez Herrera
FullStack Software Engineer | Spring Boot | AngularJS | ReactJS | Javascript | 4+ years of experience
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
The "is" operator compares the identity of two objects
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.
In the second example we can see that is False because the value is not the same, therefore the identity is different.
type() -- method returns class type of the argument(object) passed as parameter
mutable objects
A mutable object can be changed after it's created.
Common mutable type in Python:
- mutable sequences: list(), bytearray()
- set type: set()
- mapping type: dict()
- classes, class instances
Immutable objects
A immutable object cannot be changed after creating it.
Common immutable type:
- numbers: int(), float(), complex(), bool()
- 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
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.
how arguments are passed to functions and what does that imply for mutable and immutable objects
We create the variable 'a' as a list object with some data
When we call the addList(list, value) function and executes
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.
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