Is this valid ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    Is this valid ?

    hello,

    by accident I typed a double value test,
    and to my surprise it seems to work.
    Is this valid ?

    a = 2
    b = 2

    a == b == 2

    thanks,
    Stef Mientki

  • Mike Driscoll

    #2
    Re: Is this valid ?

    On Mar 19, 4:18 pm, Stef Mientki <stef.mien...@g mail.comwrote:
    hello,
    >
    by accident I typed a double value test,
    and to my surprise it seems to work.
    Is this valid ?
    >
    a = 2
    b = 2
    >
    a == b == 2
    >
    thanks,
    Stef Mientki
    It sure looks that way...

    See http://www.python.org/doc/2.3.5/ref/comparisons.html for more
    info.

    Mike

    Comment

    • Lie

      #3
      Re: Is this valid ?

      On Mar 20, 4:18 am, Stef Mientki <stef.mien...@g mail.comwrote:
      hello,
      >
      by accident I typed a double value test,
      and to my surprise it seems to work.
      Is this valid ?
      >
      a = 2
      b = 2
      >
      a == b == 2
      >
      thanks,
      Stef Mientki
      a == b == 2
      is equivalent to
      a == b and b == 2
      except that b is only evaluated once for the whole comparison

      Comment

      • Steven D'Aprano

        #4
        Re: Is this valid ?

        On Thu, 20 Mar 2008 15:09:08 +0100, Rolf van de Krol wrote:
        John Machin wrote:
        >Of course. You can chain comparisons as much as you like and is
        >(semi-)sensible, e.g.
        >>
        Hmm, 'of course' is not the correct word for it.

        Not at all. The Original Poster tried something, and it worked. There
        were two alternatives:

        (1) Writing a == b == 2 is valid.

        (2) In the sixteen years that Python has been publicly available, with
        tens of thousands or more developers using it, nobody had noticed that
        Python had a bug in the compiler which incorrectly allowed a == b == 2
        until Stef Mientki came along and discovered it.

        Given those two alternatives, (2) would be very surprising indeed, and so
        I think "of course" is well justified.

        That Python allows chaining comparisons this way isn't really surprising.
        That's a very natural thing to do. What's surprising is that other
        languages *don't* allow chaining comparisons, but force you to write the
        inefficient and (sometimes) confusing "(a == 2) and (b == 2)" instead.


        --
        Steven

        Comment

        • castironpi@gmail.com

          #5
          Re: Is this valid ?

          On Mar 20, 6:06 pm, Steven D'Aprano <st...@REMOVE-THIS-
          cybersource.com .auwrote:
          On Thu, 20 Mar 2008 15:09:08 +0100, Rolf van de Krol wrote:
          >
          John Machin wrote:
          Of course. You can chain comparisons as much as you like and is
          (semi-)sensible, e.g.
          >
          Hmm, 'of course' is not the correct word for it.
          >
          Not at all. The Original Poster tried something, and it worked. There
          were two alternatives:
          >
          (1) Writing a == b == 2 is valid.
          >
          (2) In the sixteen years that Python has been publicly available, with
          tens of thousands or more developers using it, nobody had noticed that
          Python had a bug in the compiler which incorrectly allowed a == b == 2
          until Stef Mientki came along and discovered it.
          >
          Given those two alternatives, (2) would be very surprising indeed, and so
          I think "of course" is well justified.
          >
          That Python allows chaining comparisons this way isn't really surprising.
          That's a very natural thing to do. What's surprising is that other
          languages *don't* allow chaining comparisons, but force you to write the
          inefficient and (sometimes) confusing "(a == 2) and (b == 2)" instead.
          You see a couple of occurrences in natural language-- I'm wondering
          where the majority of NL settles.
          >>a is a is a
          True

          Do we do math on booleans on some level or in some contexts? Maybe a
          "with syntaxchangeA:" is in the future.

          Comment

          • Colin J. Williams

            #6
            Re: Is this valid ?

            Lie wrote:
            On Mar 20, 4:18 am, Stef Mientki <stef.mien...@g mail.comwrote:
            >hello,
            >>
            >by accident I typed a double value test,
            >and to my surprise it seems to work.
            >Is this valid ?
            >>
            >a = 2
            >b = 2
            >>
            >a == b == 2
            >>
            >thanks,
            >Stef Mientki
            >
            a == b == 2
            is equivalent to
            a == b and b == 2
            except that b is only evaluated once for the whole comparison
            Yes - there seems to be unanimity.

            Comparisons can be chained arbitrarily,
            e.g., x < y <= z is equivalent to x < y
            and y <= z, except that y is evaluated
            only once (but in both cases z is not
            evaluated at all when x < y is found to
            be false).

            Formally, if a, b, c, ..., y, z are
            expressions and opa, opb, ..., opy are
            comparison operators, then a opa b opb c
            ....y opy z is equivalent to a opa b and
            b opb c and ... y opy z, except that
            each expression is evaluated at most once.

            Colin W.

            Comment

            Working...