Why do integers compare equal to booleans?

David Raymond David.Raymond at tomtom.com
Fri Nov 16 10:30:57 EST 2018


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.com at python.org] On Behalf Of Steve Keller
Sent: Friday, November 16, 2018 9:51 AM
To: python-list at 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


More information about the Python-list mailing list