Python Caching: == operator Vs is-operator

 

Python Caching

1
2
3
4
>>>
>>> x = 42
>>> x = 'shrubbery'
>>> 
  • Python caches and reuses small integers and small strings.
  • the object 42 here is probably not literally reclaimed; instead, it will likely remain in a system table to be reused the next time you generate a 42 in your code.
  • Most kinds of objects, though, are reclaimed immediately when they are no longer referenced;
  • The caching mechanism is irrelevant to your code.

Equality Check with == operator

1
2
3
4
5
6
7
8
>>> 
>>> L = [1,2,3]
>>> M = L                 # M and L reference the same object
>>> L == M                # Same values
True
>>> L is M                # Same objects
True
>>> 
  • The first technique here, the == operator, tests whether the two referenced objects have the same values;
  • The second method, the is operator, instead tests for object identity — it returns True only if both names point to the exact same object, so it is a much stronger form of equality testing.
  • is simply compares the pointers that implement references, and it serves as a way to detect shared references in your code if needed. It returns False if the names point to equivalent but different objects, as is the case when we run two different literal expressions:

Equality Check with is operator

1
2
3
4
5
6
7
8
>>> 
>>> L = [1,2,3]           # M and L reference different objects
>>> M = [1,2,3]
>>> L == M                # Same values
True
>>> L is M                # Different objects
False
>>> 
  • Check another case with small objects like integers:
1
2
3
4
5
6
7
8
9
10
>>> 
>>> X = 42
>>> Y = 42                # Should be two different objects
>>> 
>>> X == Y
True
>>> 
>>> X is Y                # Same object anyhow: caching at work!
True
>>> 
  • In this interaction, X and Y should be == (same value), but not is (same object) because we ran two different literal expressions (42).
  • Because small integers and strings are cached and reused, though, is tells us they reference the same single object.
  • In fact, if you really want to look under the hood, you can always ask Python how many references there are to an object: the getrefcount function in the standard sys module returns the object’s reference count.

  • This object caching and reuse is irrelevant to your code (unless you run the is check!).
  • Because you cannot change immutable numbers or strings in place, it doesn’t matter how many references there are to the same object—every reference will always see the same, unchanging value. Still, this behavior reflects one of the many ways Python op- timizes its model for execution speed.