'is not' or '!='

MRAB python at mrabarnett.plus.com
Mon Aug 18 16:53:28 EDT 2014


On 2014-08-18 21:35, ElChino wrote:
> A newbie question to you; what is the difference between statements
> like:
>    if x is not None:
> and
>   if x != None:
>
> Without any context, which one should be preferred?
> IMHO, the latter is more readable.
>
"x == y" tells you whether x and y refer to objects that are equal.

"x is y" tells you whether x and y actually refer to the same object.

In the case of singletons like None (there's only one None object),
it's better to use "is".




More information about the Python-list mailing list