[Tutor] True and 1 [was Re: use of the newer dict types]

eryksun eryksun at gmail.com
Sun Jul 28 02:33:35 CEST 2013


On Sat, Jul 27, 2013 at 4:26 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> Most of what you said I knew, but I didn't realize it went so far as
> allowing arithmetic! Now I'm on the side of that being a bad thing.
>
> Booleans should be as distinct from numbers as chars
> are from strings... ;-)

I wondered if the standard library uses arithmetic ops on bools. So I
disabled the int ops (a quick ctypes hack on PyBool_Type's tp_flags)
and ran the test suite. It turns out unittest itself uses this, so
much that I didn't even bother to finish running Python's tests. For
example:

    def three_way_cmp(x, y):
        """Return -1 if x < y, 0 if x == y and 1 if x > y"""
        return (x > y) - (x < y)

http://hg.python.org/cpython/file/516303f32bad/Lib/unittest/util.py#l78


More information about the Tutor mailing list