Re: Why do integers compare equal to booleans?
> Because Python used not to have a boolean type and used the integers 0 and 1 instead Exactly as Jon says. I wrote a post some time ago with more info about it: https://blog.rmotr.com/those-tricky-python-booleans-2100d5df92c On Fri, Nov 16, 2018 at 12:23 PM duncan smith wrote: > On 16/11/18 14:51, Steve Keller wrote: > > Why do the integers 0 and 1 compare equal to the boolean values False > > and True and all other integers to neither of them? > > > > $ python3 > > Python 3.5.2 (default, Nov 12 2018, 13:43:14) > > [GCC 5.4.0 20160609] on linux > > Type "help", "copyright", "credits" or "license" for more > information. > > >>> 0 == False > > True > > >>> 1 == True > > True > > >>> 2 == False > > False > > >>> 2 == True > > False > > >>> -1 == False > > False > > >>> -1 == True > > False > > >>> > > > > Since these are objects of different types I would expect they cannot > > be equal. I know that 0 means false and != 0 means true in C, C++, > > etc. but in Python that surprises me. > > > > Steve > > > > >>> isinstance(False, int) > True > >>> isinstance(True, int) > True > >>> False.real > 0 > >>> True.real > 1 > >>> > > At least in recent Pythons. > > Duncan > -- > https://mail.python.org/mailman/listinfo/python-list > -- Santiago Basulto.- Co-founder @ rmotr.com -- https://mail.python.org/mailman/listinfo/python-list
RE: Why do integers compare equal to booleans?
A boolean type didn't come about until version 2.3, and even now they still inherit from integers. Some links for you: https://docs.python.org/3.7/whatsnew/2.3.html#pep-285-a-boolean-type https://docs.python.org/3.7/library/stdtypes.html#boolean-values https://docs.python.org/3.7/reference/datamodel.html#the-standard-type-hierarchy -Original Message- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of Steve Keller Sent: Friday, November 16, 2018 9:51 AM To: python-list@python.org Subject: Why do integers compare equal to booleans? Why do the integers 0 and 1 compare equal to the boolean values False and True and all other integers to neither of them? $ python3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 0 == False True >>> 1 == True True >>> 2 == False False >>> 2 == True False >>> -1 == False False >>> -1 == True False >>> Since these are objects of different types I would expect they cannot be equal. I know that 0 means false and != 0 means true in C, C++, etc. but in Python that surprises me. Steve -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Why do integers compare equal to booleans?
On 16/11/18 14:51, Steve Keller wrote: > Why do the integers 0 and 1 compare equal to the boolean values False > and True and all other integers to neither of them? > > $ python3 > Python 3.5.2 (default, Nov 12 2018, 13:43:14) > [GCC 5.4.0 20160609] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> 0 == False > True > >>> 1 == True > True > >>> 2 == False > False > >>> 2 == True > False > >>> -1 == False > False > >>> -1 == True > False > >>> > > Since these are objects of different types I would expect they cannot > be equal. I know that 0 means false and != 0 means true in C, C++, > etc. but in Python that surprises me. > > Steve > >>> isinstance(False, int) True >>> isinstance(True, int) True >>> False.real 0 >>> True.real 1 >>> At least in recent Pythons. Duncan -- https://mail.python.org/mailman/listinfo/python-list
Re: Why do integers compare equal to booleans?
On 2018-11-16, Steve Keller wrote: > Why do the integers 0 and 1 compare equal to the boolean values False > and True and all other integers to neither of them? Because Python used not to have a boolean type and used the integers 0 and 1 instead, so when the boolean type was introduced True and False were made to behave very much like 1 and 0 for backwards compatibility reasons. -- https://mail.python.org/mailman/listinfo/python-list