Comparisons and singletons

Ziga Seilnacht ziga.seilnacht at gmail.com
Sat Mar 25 11:44:37 CET 2006


Steven Watanabe wrote:
> PEP 8 says, "Comparisons to singletons like None should always be done
> with 'is' or 'is not', never the equality operators." I know that "is"
> is an identity operator, "==" and "!=" are the equality operators, but
> I'm not sure what other singletons are being referred to here.

Other builtin singeltons are NotImplemented and Ellipsis, see:
http://docs.python.org/ref/types.html
for details.

>
> Also, I've seen code that does things like:
>
>   if foo is 3:
>   if foo is not '':
>
> Are these valid uses of "is"?

No. Try this examples:

>>> a = 'spam'
>>> b = ''.join(list(a))
>>> b
'spam'
>>> a == b
True
>>> a is b
False
>>> a = 10000
>>> b = 10000
>>> a == b
True
>>> a is b
False

> 
> Thanks in advance.
> --
> Steven.

Hope this helps.

Ziga




More information about the Python-list mailing list