For review: PEP 308 - If-then-else expression

Sean Ross sross at connectmail.carleton.ca
Sat Feb 8 12:30:47 EST 2003


> Sean> And, of course
>
> Sean>     if if C then x else y:
> Sean>         ...do stuff...
>
> Sean> is worse than both.
>
> I'm not sure, actually.  At least it alerts the reader that something
> is going on :-)

Now, that you mention it, yeah, it does alert the reader that something is
going on.
I suppose it's just the repitition of 'if' I don't care for...

How about:

   if when C then x else y:
       ...do stuff...

or

   if when C, x else y:
       ...do stuff...

instead?

Someone suggested the latter formulation would cause difficulties in lists
and tuples...
pfff...it's tough to think of something that would satisfy everyone isn't
it?

> Sean> Personally, I don't mind
>
> Sean>  if (y, x)[C]:
> Sean>     ...do stuff...
>
> Sean> although I don't consider it to be either obvious or clear.
>
> I *do* mind it, for two reasons:
>
> 1) It puts the false condition ahead of the true condition.

Yeah. I don't like that either, and:
    if {True: x, False:y}[C]:
        ...do stuff...
(as someone else suggested) doesn't appeal to me, although it does address
your first concern.

> 2) It evaluates both alternatives even though only one is needed.

Yep.

> Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark






More information about the Python-list mailing list