Object identities in Python

In Python there is an inbuilt function for viewing the identity of an object. Id() returns the identity of an object.

The identity of an object is a unique integer value. The identity is unique during the lifetime of an object. Furthermore, the identity will remain unchanged, i.e. constant, throughout the lifetime of an object.

Two objects with a lifespan that is not overlapping with the other objects lifespan have the same identity. The identity of an object can be thought of as its address in memory.

Difference between types and identities in Python

While the identity of an object is its address in memory, the type of an object defines what operations are possible (i.e. can be conducted with the object) and what values can be taken by the object.

Both type and identity are not changeable for a given object.

id() function and the is-operator in Python

id() is a function that returns an integer representing the identity of the object passed as argument to the function. Example:

var1 = "example string"
print(id(var1))
1427556390512

The is-operator can be used for comparing two object references to see if they are the same, i.e. have the same identity. In other words, the is-operator checks if both references are pointing to the same address in memory.

Object identity in Python when copying by reference

When copying mutable objects in Python by reference both variables hold the same reference – returning the same identity. Example:

var1 = [1,2,3] # a list is a mutable object
var2 = var1 # copying by reference
print(id(var1))
print(id(var2))
print(var1 is var2)
1427555958912
1427555958912
True

The identity is the same. Now let us try to copy an immutable object by reference. For example, a string object.

strObj1 = "Test text"
strObj2 = strObj1
print(id(strObj1))
print(id(strObj2))
print(strObj1 is strObj2)
1427556388656
1427556388656
True

At first, the situation looks the same. But a string is immutable. The difference occurs when I adjust the mutable object and immutable object.

I start by adjusting the mutable object (list) var1:

var1.append(1) # adjustment by appending additional element to list
print(var1)
print(var2)
print(id(var1))
print(id(var2))
print(var1 is var2)
[1, 2, 3, 1]
[1, 2, 3, 1]
1427555958912
1427555958912
True

Now, I do the same for the immutable object str1:

strObj1 +=  "..."
print(strObj1)
print(strObj2)
print(strObj1 is strObj2)
print(id(strObj1))
print(id(strObj2))
Test text......
Test text
False
1427557077872
1427556388656

Since a string is an immutable object type in Python, changing its value will create a new obejct with a new address in memory. I.e. a new reference.

As a result, the identity of the new object changes and the reference is no longer the same. This is why value of str2 does not change – str2 and str1 do no longer reference to the same address in memory.

You May Also Like

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.