Compare the operators is, is not with (==), (! =)

Tram Ho

1. The identifier operators is and is not

In python, is and is not are operators that compare the “identities” of two objects, namely their memory addresses. Everything in Python is an object and each object is stored at a specific memory location. In python, is and is not are operators that check to see if two variables refer to the same object in memory.
We can use id () to check the identity of any object.


There are some common cases in which objects with the same value will have the same id by default. For example, the numbers -5 to 256 are located in CPython. Each number is stored in a unique and fixed location in memory, saving memory for commonly used integers.
However, when we used the assignment operator (=) to create a variable equal to another variable, we made these variables point to the same object in memory. This can lead to undesirable behavior for mutable objects. For example in the following case:

When we change the value of variable a will lead to b is also changed according to:

The reason for this is that when assigning b to a , both b and a refer to the same address in memory, or both a and b refer to the same object.

If we define these variables independently of each other, they will be stored at different memory addresses and operate completely independently:

 

As you can see, since a and b now refer to different objects in memory, changing one object does not affect the other. And of course, the result of the is in this case will be False

2. Comparison operators (==) and (! =)

Objects with the same value are usually stored at separate memory addresses. Using the equality operators (==) and (! =) Helps us check if two objects have the same value, regardless of where they are stored in memory. In most cases, this is what we want to do. Consider the following example:

In the example above, variable b is a copy of a . We can see that although the variables a and b have the same value, they have completely different memory addresses. And in this case, the operator (==) returns True and the is operator returns False . In fact, when using the equality operator (==) , we have called the __eq __ () class method of the object on the left (==) . We absolutely can override this method

Now, SillyString’s ‘Hello world’ is equal to the string ‘World hello’ and even to any other object of the same length:

 

Of course it is very rare for us to rewrite the comparison function, but the example above illustrates what happens when you compare two objects used (==) . For the operator (! =) We have the corresponding __ne __ () function.

3. Conclusion

As a general rule, you must always use the equality operators (==) and (! =) , Unless you compare to None :

  • Use operators (==) and (! =) To compare values ​​of two objects. This is what you need if you want to compare whether two objects have the same content or not and you don’t care where they are stored in memory.
  • Use the is and is not operators when you want to compare the indentity of two objects. Here, you can compare whether two variables point to the same object in memory. The main use case for these operators is when you compare to None . It is faster and safer when compared to None by memory address than by using class methods.

In a nutshell, variables with the same value are usually stored at separate memory addresses. This means you should use (==) or (! =) To compare their values ​​and use is and is not when you want to check that two variables point to the same memory address. or not.

Share the news now

Source : Viblo