comparing booleans

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerrit Holl

    comparing booleans

    Hi,

    is it proper to compare booleans? It is possible, of course, because
    they're compatible with numbers, but booleans aren't truly numbers. I'm
    tempted to write:

    return cmp(self.extend s, other.extends)

    instead of

    if self.extends and not other.extends:
    return 1
    else:
    return -1
    # I've already verified self.extends != other.extends

    ....but somehow comparing Booleans doesn't feel right...
    Is my feeling correct?

    (Hmm, makes me wonder, for booleans, are != and ^ equal?)

    Gerrit.

    --
    174. If she bear no sons to her second husband, the sons of her first
    husband shall have the dowry.
    -- 1780 BC, Hammurabi, Code of Law
    --
    PrePEP: Builtin path type

    Asperger's Syndrome - a personal approach:


  • Erik Max Francis

    #2
    Re: comparing booleans

    Gerrit Holl wrote:
    [color=blue]
    > is it proper to compare booleans? It is possible, of course, because
    > they're compatible with numbers, but booleans aren't truly numbers.
    > I'm
    > tempted to write:
    >
    > return cmp(self.extend s, other.extends)[/color]

    Even if you're seriously worried about the correctness of comparing
    Booleans, you can always explicitly turn them into integers:

    return cmp(int(self.ex tends), int(other.exten ds))
    [color=blue]
    > (Hmm, makes me wonder, for booleans, are != and ^ equal?)[/color]

    Easily checked:
    [color=blue][color=green][color=darkred]
    >>> for x in (True, False):[/color][/color][/color]
    .... for y in (True, False):
    .... print x, y, x != y, x ^ y
    ....
    True True False False
    True False True True
    False True True True
    False False False False

    --
    __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Weakness of attitude becomes weakness of character.
    -- Albert Einstein

    Comment

    • Dang Griffith

      #3
      Re: comparing booleans

      On Wed, 28 Jan 2004 16:17:22 -0800, Erik Max Francis <max@alcyone.co m>
      wrote:
      [color=blue]
      >Gerrit Holl wrote:
      >[color=green]
      >> is it proper to compare booleans? It is possible, of course, because
      >> they're compatible with numbers, but booleans aren't truly numbers.
      >> I'm
      >> tempted to write:
      >>
      >> return cmp(self.extend s, other.extends)[/color]
      >
      >Even if you're seriously worried about the correctness of comparing
      >Booleans, you can always explicitly turn them into integers:
      >
      > return cmp(int(self.ex tends), int(other.exten ds))
      >[color=green]
      >> (Hmm, makes me wonder, for booleans, are != and ^ equal?)[/color]
      >
      >Easily checked:
      >[color=green][color=darkred]
      >>>> for x in (True, False):[/color][/color]
      >... for y in (True, False):
      >... print x, y, x != y, x ^ y
      >...
      >True True False False
      >True False True True
      >False True True True
      >False False False False[/color]

      It's not always the same, as shown here:
      [color=blue][color=green][color=darkred]
      >>> True != True != True[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> True ^ True ^ True[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> True != False != True != True[/color][/color][/color]
      False[color=blue][color=green][color=darkred]
      >>> True ^ False ^ True ^ True[/color][/color][/color]
      True

      Not that I've never used this (cool) Python syntax in practice,
      but I thought it was worth mentioning that using != and ^ in
      boolean expressions does not always give the same result. It does
      give the same result when there are only two operands.

      Interestingly, and I'm not sure why:[color=blue][color=green][color=darkred]
      >>> (True != True) != True[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> True != (True != True)[/color][/color][/color]
      True[color=blue][color=green][color=darkred]
      >>> True != True != True[/color][/color][/color]
      False

      --dang

      Comment

      • Duncan Booth

        #4
        Re: comparing booleans

        Dang Griffith <noemail@noemai l4u.com> wrote in
        news:eaa90cc908 c7a217f0d6475e4 1c7622d@news.te ranews.com:
        [color=blue]
        > Interestingly, and I'm not sure why:[color=green][color=darkred]
        >>>> (True != True) != True[/color][/color]
        > True[color=green][color=darkred]
        >>>> True != (True != True)[/color][/color]
        > True[color=green][color=darkred]
        >>>> True != True != True[/color][/color]
        > False[/color]

        You can chain comparison operators in Python. e.g.

        a < b < c

        is the same as:

        (a < b) && (b < c)

        except that if b is an expression the first form evaluates it exactly once
        whereas the second form evaluates it either once or twice.

        You can use any comparison operators in this form, so your True!=True!=Tru e
        is just shorthand for:

        (True!=True) && (True!=True)

        which is in turn equivalent to:

        False && (True!=True)

        and 'False && anything' gives False.

        Comment

        • Erik Max Francis

          #5
          Re: comparing booleans

          Dang Griffith wrote:
          [color=blue]
          > Not that I've never used this (cool) Python syntax in practice,
          > but I thought it was worth mentioning that using != and ^ in
          > boolean expressions does not always give the same result. It does
          > give the same result when there are only two operands.[/color]

          This is because of operator chaining, which only exists a special case
          for the relational operators (==, !=, <, <=, >, >=). It's not actually
          a difference in the truth table; it's because chaining operators behave
          differently than other operators.

          --
          __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
          \__/ The work will teach you how to do it.
          -- (an Estonian proverb)

          Comment

          • zde

            #6
            Re: comparing booleans

            Erik Max Francis wrote:
            [color=blue]
            > This is because of operator chaining, which only exists a special case
            > for the relational operators (==, !=, <, <=, >, >=). It's not actually
            > a difference in the truth table; it's because chaining operators behave
            > differently than other operators.[/color]

            Unfortunately, this mis-feature works for other operators as well:
            Since 'in' operator usually has higher precedence than '==', it's
            pretty annoying to see:

            Python 2.3.3 (#2, Jan 4 2004, 12:24:16)[color=blue][color=green][color=darkred]
            >>> 'a' in 'abc'[/color][/color][/color]
            True[color=blue][color=green][color=darkred]
            >>> 'a' in 'abc' == True[/color][/color][/color]
            False[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Comment

            Working...