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

Jim jbublitzNO at SPAMnwinternet.com
Sat Feb 8 14:57:03 EST 2003


Aahz wrote:
> In article <mailman.1044638179.4983.python-list at python.org>,
> Guido van Rossum  <guido at python.org> wrote:

>>   The proposed syntax is as follows:

>>       <expression1> if <condition> else <expression2>

> I've been flipping between -1 and +1 (going through -0 and +0) as various
> arguments are put forth.  What's put me mostly in the -1 camp is the use
> of the ternary operator in an if statement.  That's just too ugly for
> words.  I would be willing to abstain from (or possibly even vote for) a
> proposal that reads left-to-right *and* doesn't muck with the if
> statement.  So far, the only proposals that work on those grounds are

>     <cond> ? <exp1> : <exp2>

> and

>     when <cond> then <exp1> else <exp2>

This is a real question, not rhetorical (and
maybe naive and maybe one that's already been
asked):

Why is

     when <cond> then <exp1> else <exp2>

or

     <expression1> if <condition> else <expression2>

different or better than:

     def when(cond, exp1, exp2):
         if cond:
             return exp1
         else:
             return exp2

     x = when(a, b, c)
     if when(a, b, c):
         <do something>
     else:
         <do something else>

and making 'when' built-in??

And if nobody has a regular habit of adding
this function to every module they write, why
is the need for a ternary operator pressing?

I used to be in the habit of adding:

     FALSE = 0
     TRUE  = not FALSE

to the top of everything, so the new boolean
stuff actually saves me time (and saves me
when I forget to add the above). I guess I'd
lean towards wanting to see the same kind of
reason for adding a ternary operator.

I'm not sure which side of the argument I'm on,
but I am curious about both questions above. I
use the C ternary operators a lot, and missed
them in Python a little at first, but never
enough to write a function to replace them.

My only other contribution (caused by
misreading someone else's post) is to suggest
the syntax:

     <cond> asif <exp1> whatever <exp2>

I'm not really serious, but it is kind of catchy.


Jim






More information about the Python-list mailing list