471,863 Members | 912 Online

# Comparisons and singletons

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"?

--
Steven.
Mar 25 '06 #1
6 1294
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

--
Steven.

Hope this helps.

Ziga

Mar 25 '06 #2
"Ziga Seilnacht" <zi************@gmail.com> wrote in message
a = 10000
b = 10000
a == b True a is b False

and noticed
a = 10
b = 10
a is b True

Why the difference?

2. If I really want a value True will I ever go astray with the test:
if a is True: a = True
b = 1.
c = 1
a is True, b is True, c is True

(True, False, False)

Thanks,
Alan Isaac
Mar 25 '06 #3
On 3/25/06, David Isaac <ai*****@verizon.net> wrote:
"Ziga Seilnacht" <zi************@gmail.com> wrote in message
>> a = 10000
>> b = 10000
>> a == b True
>> a is b

False

and noticed
a = 10
b = 10
a is b True

Why the difference?

2. If I really want a value True will I ever go astray with the test:
if a is True: a = True
b = 1.
c = 1
a is True, b is True, c 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.
Thanks,
Alan Isaac
--
http://mail.python.org/mailman/listinfo/python-list

Mar 25 '06 #4
David Isaac wrote:
"Ziga Seilnacht" <zi************@gmail.com> wrote in message
>> a = 10000
>> b = 10000
>> a == b True
>> a is b

False

and noticed
a = 10
b = 10
a is b 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: a = True
b = 1.
c = 1
a is True, b is True, c 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

Mar 25 '06 #5
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:
a = 10
b = 10
a is b True a = 100
b = 100
a is b False a = 50
b = 50
a is b True a = 70
b = 70
a is b True a = 99
b = 99
a is b True

And to the other side:
a = -10
b = -10
a is b False a = -5
b = -5
a is b True a = -6
b = -6
a is b

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/pytho...ts/intobject.c
[2] http://svn.python.org/projects/pytho...ts/intobject.c

HTH,

--
Felipe.

Mar 25 '06 #6
2. If I really want a value True will I ever go astray with the test:
if a is True:
>> a = True
>> b = 1.
>> c = 1
>> a is True, b is True, c is True (True, False, False)

"Ziga Seilnacht" <zi************@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com... 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 wrote: 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
Mar 26 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.