Comparisons and singletons

Ziga Seilnacht ziga.seilnacht at gmail.com
Sat Mar 25 12:11:51 EST 2006


David Isaac wrote:
> "Ziga Seilnacht" <ziga.seilnacht at gmail.com> wrote in message
> news:1143283477.483035.212980 at v46g2000cwv.googlegroups.com...
> > >>> a = 10000
> > >>> b = 10000
> > >>> 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




More information about the Python-list mailing list