Python The difference between not and! =

I wondered what the difference is between these two pieces of code:

while choice != "y" and choice != "n": while not choice == "y" and not choice == "n": 
+4
source share
1 answer

The first uses __ne__ objects, and the second uses __eq__ objects and negates its result.

While both methods should combine their result, therefore a == b implies not a != b , this is not actually required or forced.

There are no implied relationships between comparison operators. True x==y does not mean that x!=y is false. Accordingly, when defining __eq__() should also define __ne__() so that the operators act properly.

In your case, where choice is a string (I think?), That doesn't really matter. Built-in types provide feedback between == and != . So the only difference is how clear this is when viewing the code. And I personally prefer a shorter first version.

+9
source

Source: https://habr.com/ru/post/1481755/


All Articles