Is this valid ?

Discussion in 'Python' started by Stef Mientki, Mar 19, 2008.

  1. Stef Mientki

    Stef Mientki Guest

    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
     
    Stef Mientki, Mar 19, 2008
    #1
    1. Advertising

  2. On Mar 19, 4:18 pm, Stef Mientki <> wrote:
    > 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
     
    Mike Driscoll, Mar 19, 2008
    #2
    1. Advertising

  3. Stef Mientki

    John Machin Guest

    On Mar 20, 8:18 am, Stef Mientki <> wrote:
    > 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
    >


    Of course. You can chain comparisons as much as you like and is
    (semi-)sensible, e.g.

    assert 0 < thing_index < thing_count <= UTTER_MAX_NTHINGS

    There was an interesting use of chained comparison within the last day
    or 2 in a thread about deriving a set of duplicated list elements --
    look for subject == "finding items that occur more than once in a
    list" and author == "Arnaud Delobelle".

    Cheers,
    John
     
    John Machin, Mar 19, 2008
    #3
  4. Stef Mientki

    Lie Guest

    On Mar 20, 4:18 am, Stef Mientki <> wrote:
    > 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
     
    Lie, Mar 20, 2008
    #4
  5. 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. Although the Stef
    Mientki would probably be able to find it in the documentation it is not
    as simple as you might think.
    Most languages interpret a == b == 2 as (a == b) == 2, or throw an error
    because this syntax is not valid. The fact that python understand the
    obvious meaning of this code, is quite unique to Python, as far as I know.
     
    Rolf van de Krol, Mar 20, 2008
    #5
  6. Stef Mientki

    John Machin Guest

    On Mar 21, 1:09 am, 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.


    'Of course' was short for: Given alternative hypotheses H0 = "Python
    feature" and H1 = "bug in Python compiler", one's instinct would be go
    with H0 if a snap decision were required without time to refer to the
    docs.

    > Although the Stef
    > Mientki would probably be able to find it in the documentation it is not
    > as simple as you might think.
    > Most languages interpret a == b == 2 as (a == b) == 2, or throw an error
    > because this syntax is not valid.


    Indeed, some languages do ludicrous things with precedence of
    relational operators. A compiler when faced with something like:
    a == b and c == d
    has at least two choices:
    (1) (a == b) and (c == d) # seems rather useful
    (2) (a == (b and c)) == d
    which doesn't seem very useful at all. Pascal makes choice (2), which
    is valid syntax only if b and c are booleans and if comparisons of
    booleans are allowed (and IIRC Pascal didn't allow this).

    > The fact that python understand the
    > obvious meaning of this code, is quite unique to Python, as far as I know.


    Indeed, Python isn't "some/most languages", which is why we're here.
     
    John Machin, Mar 20, 2008
    #6
  7. 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
     
    Steven D'Aprano, Mar 20, 2008
    #7
  8. Stef Mientki

    Guest

    On Mar 20, 6:06 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > 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.
     
    , Mar 21, 2008
    #8
  9. Lie wrote:
    > On Mar 20, 4:18 am, Stef Mientki <> wrote:
    >> 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.
     
    Colin J. Williams, Mar 21, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    605
  2. Ajit
    Replies:
    1
    Views:
    3,644
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. CT

    vbc is not valid command

    CT, Oct 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,862
  4. Pial
    Replies:
    6
    Views:
    52,460
    Nassos
    Apr 28, 2005
  5. MM
    Replies:
    3
    Views:
    452
    Kai Harrekilde-Petersen
    Jun 19, 2004
Loading...

Share This Page