a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Sun Jun 8 06:55:46 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> writes:

>>A conditional operator would be nice, but contravene the important
>>rule that statements (if, return, etc) can skip over blocks of code
>>and control what is evaluated, while expressions cannot.  [...]
>
>The existing logical operators short-circuit:
>
>    >>> 0 and 1/0
>    0
>    >>> 1 or 1/0
>    1

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.

But you're right, they do short-circuit, and so there is no need for a
separate conditional operator (provided you consider the and-or trick
a readable idiom).

-- 
Ed Avis <ed at membled.com>




More information about the Python-list mailing list