Comparisons and singletons

2006-03-25 Thread 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.
--
Steven.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparisons and singletons

2006-03-25 Thread Ziga Seilnacht
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 = 1
>>> b = 1
>>> a == b
True
>>> a is b
False

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

Hope this helps.

Ziga

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparisons and singletons

2006-03-25 Thread David Isaac
"Ziga Seilnacht" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> >>> a = 1
> >>> b = 1
> >>> a == b
> True
> >>> a is b
> False

Two follow up questions:

1. I wondered about your example,
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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparisons and singletons

2006-03-25 Thread Chris Mellon
On 3/25/06, David Isaac <[EMAIL PROTECTED]> wrote:
> "Ziga Seilnacht" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > >>> a = 1
> > >>> b = 1
> > >>> a == b
> > True
> > >>> a is b
> > False
>
> Two follow up questions:
>
> 1. I wondered about your example,
> 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
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparisons and singletons

2006-03-25 Thread Ziga Seilnacht
David Isaac wrote:
> "Ziga Seilnacht" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > >>> a = 1
> > >>> b = 1
> > >>> a == b
> > True
> > >>> a is b
> > False
>
> Two follow up questions:
>
> 1. I wondered about your example,
> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Comparisons and singletons

2006-03-25 Thread 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:
>>> 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/python/tags/release24-fork/Objects/intobject.c 
[2] http://svn.python.org/projects/python/trunk/Objects/intobject.c

HTH,

-- 
Felipe.

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Comparisons and singletons

2006-03-26 Thread David Isaac
Alan asked:
> > 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" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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


-- 
http://mail.python.org/mailman/listinfo/python-list