comparing booleans

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Jan 28 18:13:09 EST 2004


> From: Gerrit Holl
> 
> is it proper to compare booleans? It is possible, of course, because
> they're compatible with numbers, but booleans aren't truly 
> numbers.

Actually, they *are* really numbers ...

    >>> isinstance(True, int)
    True
    >>> isinstance(False, int)
    True
    >>> True == 1
    True
    >>> False == 0
    True
    >>> True == 2
    False

Note the last case - you can't just compare any true value with True and expect that the comparison will compare True - it must explicitly equal 1 (or True). Likewise with False.

However, if `extends` is definitely boolean in your example, then it's perfectly reasonable to do:

    return cmp(self.extends, other.extends)

if you want False to be considered less than True.

Tim Delaney




More information about the Python-list mailing list