Python Data Models Explained — Object, Type & Identity

Praveen Kumar
4 min readNov 26, 2022

--

Every python data is an object! What do I mean by that? Let’s find out.

The official documentation says that “Objects are Python’s abstraction of data”. 😅 Meaning? To understand such a statement let’s know what abstraction actually means. Abstract means “just the idea but not as a physical thing”. So in simpler terms, without knowing every detail of how things can be done, abstraction is a way to represent an idea. Thus, the statement “Objects are Python’s abstraction of data” means that “Python language uses the concept of Objects to represent all their data”.

Simple enough? Let’s move on.

Now we know that every data representation can be understood by its underlying object representation. So, if we create an integer variable in python, it would be called an integer object. The same applies to all the different variables and data types you can construct or think of.

Whenever we create an object, we associate 3 properties with it.

  1. The value of an object
  2. The location or Identity of an object
  3. The type of an object

For example, when we define: A = 10, we have all these 3 properties bound with it. Note that A is just a variable(reference/pointer) & not an object. 10 is the object. In python, 10 (an object) is first created into the Heap Segment (Since it’s a global variable), and then a reference variable ‘A’ is created which points to the memory address where 10 is created. Thus A is just a mere reference to the object. The same principle is followed across every python object.

When ‘10' was created, the 3 properties are defined as follows:

  1. Value: 10
  2. Location: 0x238A352 (Some memory address)
  3. Type: Integer

This is the reason python only creates a single object of 10. If later you create another variable with the same value, the address of the of 10 is again assigned to the new variable without having to create another object of the same value. In CPython implementation, there is an inbuild method id(variable/object) which is just to find out the memory address of a variable/object. let’s look at the following code example.

This shows ‘a’ and 10 and ‘c’, all of them have the same memory location. This shows that only 1 instance of 10 was created, and then it was assigned to ‘a’ & ‘c’.

Python allows us to compare two values with the ‘==’ operator. However, to compare if two variables are pointing/referencing to the same memory location, we have to use the is operator.

Here, when we do a == c, the values are compared. However, when we do a is c, it internally compares id(a) == id(c).

Along with the identity/location of an object, the type of the object is helpful in deciding the operations, limitations, and methods that are associated with the object. For example, the length can be calculated for a string object but not for float or integer type.

Objects whose values can be changed are called mutable objects and objects whose values can not be changed once created are called immutable objects. However, immutability is not just the essence of unchangeable objects, it's more subtle. Let’s take an example to explain what I mean by that.

Official python documentation states the following data types.

Python Data Types & their Mutability

Consider the following program now…

if we see, we have a tuple object and it contains a list object. This is an example of an Immutable container (A container is an object which can contain references to other objects) containing a mutable object. We can change the inner list but we can not still change the immutable container which contains it. Even though you may feel that the tuple is changed, it is not the tuple but the mutable object inside it changed. This is why python is a live language because it can allow you to such complex behavior in a more subtle way.

Learning always pays off. Stay patient & keep learning. 📚

If you got value from this article, share it on your Social Media handles. 😃

--

--

Praveen Kumar

Senior AI Engineer - Building Products for Organizations, gaining Experience, and sharing it out on social media.