Identity Operators in Python

 


In Python, identity operators are used to compare the memory locations (addresses) of two objects. They are used to check if two objects refer to the same location in memory.

Identity Operators

There are two identity operators in Python:

  1. is: Returns True if two variables refer to the same object in memory.

  2. is not: Returns True if two variables refer to different objects in memory.

Syntax:

  • is:
    variable1 is variable2


  • is not:
    variable1 is not variable2


Examples:

Using is Operator:

The is operator checks whether two variables refer to the same object in memory (i.e., whether they have the same identity).

  • x = [1, 2, 3]

  • y = x  # y refers to the same object as x

  • z = [1, 2, 3]  # z refers to a different object even though it has the same value


  • print(x is y)  # Output: True (x and y refer to the same object)

  • print(x is z)  # Output: False (x and z refer to different objects)


Using is not Operator:

The is not operator checks whether two variables refer to different objects in memory.

  • x = [1, 2, 3]

  • y = [1, 2, 3]


  • print(x is not y)  # Output: True (x and y refer to different objects in memory)


Comparison with Equality (==) Operator:

  • The == operator checks if the values of two objects are the same, whereas the is operator checks if two objects refer to the same memory location.

  • For example, two different objects with the same value will be considered equal (==), but they will not be the same object in memory (is).

  • a = [1, 2, 3]

  • b = [1, 2, 3]


  • print(a == b)   # Output: True (values are the same)

  • print(a is b)   # Output: False (different objects in memory)


Practical Example:

  • x = "hello"

  • y = "hello"

  • z = "world"


  • print(x is y)   # Output: True (because of string interning, both refer to the same object)

  • print(x is z)   # Output: False (different objects in memory)


In Python, small strings and numbers are often "interned," meaning Python reuses the same object for identical immutable objects to save memory. Hence, the comparison using is might return True for strings like "hello", as they refer to the same object in memory.

Use Cases:

  • Checking Object Identity: The is operator is useful when you want to check if two variables point to the same object, especially in the case of mutable and immutable objects.

  • Comparison with None: It's common to use the is operator when comparing a variable to None, as None is a singleton object.

  • x = None

  • if x is None:

  •     print("x is None")


Conclusion:

  • The is operator checks if two variables point to the same object in memory.

  • The is not operator checks if two variables point to different objects in memory.

  • These operators are useful for identity comparisons, especially with objects like None, singletons, and when comparing object references.

Let me know if you'd like more examples or further clarification! 😊


Post a Comment

0 Comments