Objects, Types and Values. The de-facto of Python language | by Farhad Naeem | Towards Data Science

“One of the best qualities of Python is its consistency. After working with Python for a while, you are able to start making informed, correct guesses about features that are new to you.”
“Python data model, which describes the API that you can use to make your own objects play well with the most idiomatic language features.” — Fluent Python, Luciano Ramalho
The Python data model can be defined as the ‘Python Framework’ or ‘Python Design Philosophy’. The developers of the language have dedicated fairly bigger documentation that might be a bit ambiguous, intimidating, or vague at the first look.
However, in simple words, the term data model is self-explanatory — it deals with how Python organizes everything internally to treat and process data as well as some high-level concepts of its core design and philosophy. It discusses the basic building blocks of the language itself, the design/structure of the building blocks, and the basic code-blocks that come into play with them.
In this chapter, we will talk about Objects, types, and values
In Python — Everything is an object. Each object has an identity, a type, and a value
In Python — Everything is an object. Each object has an identity, a type, and a value. So, what is the fuss with identity, type, and value? We are going to write some simple code —
>>> player_1 = "Maradona">>> id(player_1)139780790567600>>> age = 53>>> id(age)9753824
We can think of identity as an object’s address in memory
Here, the id() gives us the identity that we were talking about, which is a unique integer that never changes during the lifetime of an object. We can think of identity as an object’s address in memory. We can check if two objects are the same with is operator.
>>> player_2 = "Pele">>> player_1 is player_2     #They are not referring the same objectFalse >>> player_copy = player_1>>> player_1 is player_copy   #They are referring the same objectTrue>>> id(player_2)139780790567984>>> id(player_copy)139780790567600
Now, if we want to check the type of these objects, we can use type().
>>> type(age)<class ‘int’>>>> type(player_1)<class 'str'>
The type of an object cannot change
The type of an object cannot change1
1It is possible in some cases to change an object’s type, under certain controlled conditions. It generally isn’t a good idea though, since it can lead to some very strange behavior if it is handled incorrectly.
Type specifies two things: – what operations are allowed – the set of values the object can hold
The value of an object can be changed if the object is mutable and if the object is immutable the value of it cant be changed.
And, The value of an object is what the object is holding in it. Here 53 is the value of age and Maradona is the value of player_1 . The value of an object can be changed if the object is mutable and if the object is immutable the value of it cant be changed.
age = 53 is an integer type object with value 53 and has the id 9753824
That means the age = 53 is an integer type object with value 53 and has the id 9753824 and if we now change the age to 54 , it will be referring to a different object
>>> age = 54 # here, age is referring to a different object>>> id(age)  # showing different identity than the previous one 9753856
So, if we want to change the value of player_1 then both objects player_copy and player_1 will not show the same identity
>>> player_1 = "Messi" # assigning new value to >>> player_copy'Maradona'>>> player_1 is player_copyFalse
So, both player_1 aka string and age aka integers are immutable types. There are other immutable types. such as — integers, floats, strings, tuples. On the other hand, mutable types are lists, dictionaries, and sets. Now, looking into the mutable types
>>> players = ["Messi", "Maradona", "Pele"]>>> players_copy = players     # Coping the reference to the list                                object,not the list object itself.>>> players is players_copyTrue>>> players[0]="Ronaldo"          # Changing the first element>>> players['Ronaldo', 'Maradona', 'Pele']>>> players_copy['Ronaldo', 'Maradona', 'Pele']   # They refer to same list object.>>> players.append("Pirlo")       # Appending a new element>>> players['Ronaldo', 'Maradona', 'Pele', 'Pirlo']>>> players_copy['Ronaldo', 'Maradona', 'Pele', 'Pirlo']>>> players_copy.append("Cruyf")>>> players_copy['Ronaldo', 'Maradona', 'Pele', 'Pirlo', 'Cruyf']>>> players['Ronaldo', 'Maradona', 'Pele', 'Pirlo', 'Cruyf']
As both players and players_copy referring to the same list object, changes in one list will have an effect on the other variable.
In the above example, we mutated the players list by changing the first value and using append() method. As both players and players_copy referring to the same list object, changes in one list will have an effect on the other variable.
>>> id(players)139780790567040>>> id(players_copy)139780790567040
Note that, if we change the list a variable referring to, it will create a new list object. We shouldn’t get confused between mutating and changing a variable.
>>> players = ["Messi", "Maradona", "Pele"]>>> id(players)139780790567040>>> players = ["Messi", "Maradona", "Pele", 'Pirlo', 'Cruyf']>>> id(players) 139780790568064
Here, this line players = [“Messi”, “Maradona”, “Pele”, ‘Pirlo’, ‘Cruyf’] creating a new list and referring players variable into it. We can tell by checking both the ids.
To get a more clear understanding of mutability and immutability, let's bring tuple in the firing zone. As we said earlier, tuples are immutable, mutating tuples types shouldn't be possible.
>>> players_tuple = ("Kaka", "Buffon")>>> players_tuple('Kaka', 'Buffon')>>> type(players_tuple)<class 'tuple'>>>> id(players_tuple)139780790517248>>> players_tuple[0]'Kaka'>>> players_tuple[0]="De Bruyne"     # can't mutate in tuplesTraceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: 'tuple' object does not support item assignment>>> players_tuple= players_tuple +("De Bruyne",) # adding a new data>>> players_tuple('Kaka', 'Buffon', 'De Bruyne')>>> id(players_tuple)                # it's creating a new tuple139780766363584
In this part, we tried to get a clear understanding of how an object holds its essence in the pythonic world and the differences between mutability and immutability. In the later chapters, we will dive more into the python data model.
References:1. https://docs.python.org/3/reference/datamodel.html2. Fluent Python by Luciano Ramalho. This is the book every Pythonista should read to sharpen their python skills.