a = b = 1 just syntactic sugar?

Steven Taschuk staschuk at telusplanet.net
Sun Jun 8 17:48:21 CEST 2003


Quoth Ed Avis:
  [...]
> Oh.  I thought someone said on this newsgroup only a few days ago that
> they did not, when I mentioned that
> 
>     cond and x or y
> 
> is a well-known substitute for a conditional operator like C's
> (cond ? x : y) or Haskell's (if cond then x else y).  Someone said
> that it wouldn't work, because x and y would be evaluated in either
> case.

No.  Here's what Terry said:

    "This conditional selection expression (which I have used) works as a
    substitute for part of certain if-else statements if and only if 
    bool(if_expression) == True [...]"

By "if_expression" here he means what you call "x" above.  Note
what happens if x is false:

    >>> cond = True
    >>> cond and 0 or 1
    1

This form works only for true x.  There's another form, as Terry
discovered back in February, which works for false x (and only for
false x).  But obviously one would prefer an expression which has
the desired behaviour no matter what x is.

-- 
Steven Taschuk                               staschuk at telusplanet.net
"What I find most baffling about that song is that it was not a hit."
                                          -- Tony Dylan Davis (CKUA)





More information about the Python-list mailing list