conditional expressions (RE: Loop-and-a-half (Re: Curious assignment behaviour))

Andrew Dalke dalke at dalkescientific.com
Mon Oct 15 03:48:16 EDT 2001


Chris Tavares wrote:
>May I be the first to say - ICK!

I'll second that nomination.

It looks ugly, and doesn't solve any problems I have.
I *like*

  if e1:
    x = e2
  else:
    x = e3

Makes it easy to see what goes on when e1 is true or false.
Take Uncle Tim's example code

  x = (if a+b/sqrt(3) then 3**f(5, 3)- 12 else ",".join(list) + ":\n")

and compare its readability to

  if a+b/sqrt(3):
    x = 3**f(5, 3) - 12
  else:
    x = ",".join(list) + ":\n"

I can easily understand the second.  The first takes effort.

There's talk of generalization.  But more likely the expansion of a
one-liner like the above is to add more statements to a branch, as
in something like:

  if a+b/sqrt(3):
    count = count + 1
    x = 3**f(5, 3) - 12
  else:
    spam *= count
    x = ",".join(list) + ":\n"

With the one-liner form, translation to the normal Python if/else
statement calls for a lot of code changes.

I tried looking through the thread in its various subject names.
I couldn't figure out what was the driving reason for this idea.
(Other than as a replacement for C's ?:  ternary operator.)
Enlightment, anyone?

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list