The is operator for large integers in Python
In Python, we use the is operator for an object’s identity comparison. It checks whether two variables refer to the same object in memory. If the objects are identical, is returns True; otherwise, it returns False.
Here’s a code example:
x = 100y = 100print(x is y) # Output: True
In the code above, we use the is operator to compare x and y and we get the output True. We can check the ids of both variables using the id() function as shown in the following widget:
x = 100y = 100print(id(x))print(id(y))
The ids (memory addresses) of x and y are the same because the id() function in Python returns the unique identifier of an object. It provides the memory address to where the object is stored in the computer’s memory.
Integer caching
Python caches and reuses the same objects for the commonly used integers ranging from -5 to 256 to optimize memory usage and improve performance. This process is known as integer caching. So, when we use the is operator to compare two integer literals or variables within this range, we get True if they have the same value.
Here’s a code example that illustrates this behavior:
x = 100y = 100print(x is y) # Output: Trueprint(id(x))print(id(y))x += 100y += 100print(x is y) # Output: Trueprint(id(x))print(id(y))x += 100y += 100print(x is y) # Output: Trueprint(id(x))print(id(y))
Explanation
-
Lines 1–2: We assign the value
100to the variablesxandy. -
Line 3: We check if
xandyrefer to the same object in memory using theisoperator. Both variables contain the same value, which is an integer and since small integers are typically cached in memory, they refer to the same object. Therefore, the output will beTrue. -
Lines 4–5: We print the memory addresses (identity) of the objects referred to by
xandy. Sincexandyboth contain the same integer value, they refer to the same object and so, the memory address will also be the same. -
Lines 6–7: We increment the values of
xandyby100. After these lines, bothxandywill have the value200. -
Line 8: We again check if
xandyrefer to the same object in memory. Since both variables now contain the value200, they still refer to the same object. Therefore, the output will beTrue. -
Lines 9–10: We print the memory addresses (identities) of the objects referred to by
xandy. Sincexandyboth contain the same integer value, they refer to the same object. Therefore, the memory address will be the same. -
Lines 11–12: We increment the value of
xandyagain by100. After these lines,xandywill have the value300. -
Line 13: We again check if
xandyrefer to the same object in memory. Since both variables now contain the value300, which is out of the range-5to256, the output will beFalse. -
Lines 14–15: We print the memory addresses (identities) of the objects referred to by
xandy. In this case, the memory addresses are different due to the increment.
Identity comparison of variables and literal values
As we have already seen, assigning values exceeding the range of -5 to 256 will output False, when we use the is operator to compare them. It is the same for literal values.
Here’s a code example:
x = 300y = 300print(x is y) # Output: Trueprint(300 is 300) # Output: True
Explanation
In the above example, the x and y values exceed the range -5 to 256, but the output is still True because Python
keeps an array of integer objects for all integers between -5 and 256. When we create an int in that range, we actually get back a reference to the existing object. But when we increment a variable with a constant value, and if the value of the integer exceeds the range -5 to 256 after the increment, they become separate objects in the memory. So, the output will then be False.
Free Resources