Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison in Python: is vs. == [duplicate]

I noticed a Python script I was writing was acting squirrelly, and traced it to an infinite loop, where the loop condition was while line is not ''. Running through it in the debugger, it turned out that line was in fact ''. When I changed it to !='' rather than is not '', it worked fine.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values? I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

like image 592
Coquelicot Avatar asked Jun 07 '10 08:06

Coquelicot


People also ask

Can Python compare strings with ==?

Python comparison operators can be used to compare strings in Python. These operators are: equal to ( == ), not equal to ( != ), greater than ( > ), less than ( < ), less than or equal to ( <= ), and greater than or equal to ( >= ).

Can we use == for string comparison?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What is the difference between IS and == in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory.

When comparing strings Why is == not a good idea?

Do not use the == operator to compare Strings. Note: When comparing two strings in java, we should not use the == or != operators. These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer.


2 Answers

For all built-in Python objects (like strings, lists, dicts, functions, etc.), if x is y, then x==y is also True.

Not always. NaN is a counterexample. But usually, identity (is) implies equality (==). The converse is not true: Two distinct objects can have the same value.

Also, is it generally considered better to just use '==' by default, even when comparing int or Boolean values?

You use == when comparing values and is when comparing identities.

When comparing ints (or immutable types in general), you pretty much always want the former. There's an optimization that allows small integers to be compared with is, but don't rely on it.

For boolean values, you shouldn't be doing comparisons at all. Instead of:

if x == True:     # do something 

write:

if x:     # do something 

For comparing against None, is None is preferred over == None.

I've always liked to use 'is' because I find it more aesthetically pleasing and pythonic (which is how I fell into this trap...), but I wonder if it's intended to just be reserved for when you care about finding two objects with the same id.

Yes, that's exactly what it's for.

like image 196
dan04 Avatar answered Oct 12 '22 21:10

dan04


I would like to show a little example on how is and == are involved in immutable types. Try that:

a = 19998989890 b = 19998989889 +1 >>> a is b False >>> a == b True 

is compares two objects in memory, == compares their values. For example, you can see that small integers are cached by Python:

c = 1 b = 1 >>> b is c True 

You should use == when comparing values and is when comparing identities. (Also, from an English point of view, "equals" is different from "is".)

like image 36
pygabriel Avatar answered Oct 12 '22 19:10

pygabriel