“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 object
False >>> player_copy = player_1
>>> player_1 is player_copy #They are referring the same object
True>>> 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 change¹
¹It 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_copy
False
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_copy
True>>> 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']
#python #programming #developer