"0 in [True,False]" returns True

Fredrik Lundh fredrik at pythonware.com
Tue Dec 13 10:45:09 CET 2005

Erik Max Francis wrote:

> > What's the point of having Booleans, if you can't tell them from integers?
> Because
> return True
> is clearer than
> return 1
> if the purpose of the return value is to indicate a Boolean rather than
> an arbitrary integer.

the real reason booleans were added was that sillyness like

    True = 1 == 1
    False = not True


    return 1 # true


    class Boolean:

        def __init__(self, value = 0):
            self.value = operator.truth(value)

        def __cmp__(self, other):
            if isinstance(other, Boolean):
                other = other.value
            return cmp(self.value, other)

        def __repr__(self):
            if self.value:
                return "<Boolean True at %x>" % id(self)
                return "<Boolean False at %x>" % id(self)

        def __int__(self):
            return self.value

        def __nonzero__(self):
            return self.value

    True, False = Boolean(1), Boolean(0)

were all too common in the wild.

for the full story, see


and, to briefly return to the original topic, note that

    "This PEP does *not* change the fact that almost all object types
    can be used as truth values.  For example, when used in an if
    statement, an empty list is false and a non-empty one is true;
    this does not change and there is no plan to ever change this.

    The only thing that changes is the preferred values to represent
    truth values when returned or assigned explicitly.  Previously,
    these preferred truth values were 0 and 1; the PEP changes the
    preferred values to False and True, and changes built-in
    operations to return these preferred values."

in general, returning True and False is pythonic, explicitly testing for
them is not.


More information about the Python-list mailing list