Comparisons and singletons

S

Steven Watanabe

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.

Also, I've seen code that does things like:

if foo is 3:
if foo is not '':

Are these valid uses of "is"?

Thanks in advance.
 
Z

Ziga Seilnacht

Steven said:
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:
False


Thanks in advance.

Hope this helps.

Ziga
 
D

David Isaac

Ziga Seilnacht said:

Two follow up questions:

1. I wondered about your example,
and noticedTrue

Why the difference?

2. If I really want a value True will I ever go astray with the test:
if a is True:(True, False, False)

Thanks,
Alan Isaac
 
C

Chris Mellon

Two follow up questions:

1. I wondered about your example,
and noticed
True

Why the difference?

2. If I really want a value True will I ever go astray with the test:
if a is True:
(True, False, False)

None, True, and False are all singletons and should be compared with
"is". There are some other singletons - small integers (up to 10, I
believe) as well as the empty string. However, I am not sure (and I am
sure someone will correct me if I'm wrong) but I believe that these
are not specified as singletons, and that it's an implementation
detail that they are.
 
Z

Ziga Seilnacht

David said:
Two follow up questions:

1. I wondered about your example,
and noticed
True

Why the difference?

Python has a special internal list of integers in which it caches
numbers smaller than 1000 (I'm not sure that the number is correct),
but that is an implementation detail and you should not rely on it.
2. If I really want a value True will I ever go astray with the test:
if a is True:
(True, False, False)

I think that True and False, although they were added in version
2.3, were not true singeltons until version 2.4. You should finish
reading the PEP, see especially this part:

- Don't compare boolean values to True or False using ==

Yes: if greeting:

No: if greeting == True:

Worse: if greeting is True:
Thanks,
Alan Isaac

Ziga
 
F

Felipe Almeida Lessa

Em Sáb, 2006-03-25 às 09:11 -0800, Ziga Seilnacht escreveu:
Python has a special internal list of integers in which it caches
numbers smaller than 1000 (I'm not sure that the number is correct),
but that is an implementation detail and you should not rely on it.

By testing:True

And to the other side:
False

And then, when looking to Python 2.4's code[1]:
"""
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 100
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
"""

However, as stated before, don't rely on these numbers. The trunk[2] defines now 256, not 99, as the biggest integer shared.

[1]
http://svn.python.org/projects/python/tags/release24-fork/Objects/intobject.c
[2] http://svn.python.org/projects/python/trunk/Objects/intobject.c

HTH,
 
D

David Isaac

Alan asked:
Ziga Seilnacht said:
I think that True and False, although they were added in version
2.3, were not true singeltons until version 2.4.

OK, but Python 2.3 yields the same result as above.

Ziga said:
You should finish
reading the PEP, see especially this part:
- Don't compare boolean values to True or False using ==
Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:


I do not think this is relevant to the question I asked,
which was how to test for a value of True, if that's
what I really want. I think the outcome of this
discussion has been: use 'is'.

Thanks,
Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,134
Latest member
JavierIlif

Latest Threads

Top