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

Dan Schmidt dfan at dfan.org
Sat Feb 8 12:52:34 EST 2003


aahz at pythoncraft.com (Aahz) writes:

| While true in a technical sense, I'd expect there are a number of
| people who are reading this thread who didn't really understand how
| this construct works prior to this thread, and who prefer to protect
| other people from being forced to learn.

One thing that I don't think has been pointed out about the proposed
syntax is that it is a lot easier to understand upon encountering it
for the first time than C's ?: is.

I think that if someone who isn't familiar with the syntax encounters
the statement

   x = sqrt(y) if y >= 0 else 0

he will still be able to figure out what's going on without too much
effort, while

   x = y >= 0 ? sqrt(y) : 0

is pretty much gibberish until someone explains the syntax.

Meanwhile, I have had to maintain code in which people write

   x = y >= 0 and sqrt(y) or 0

which is possibly the worst of the three.

I know that people are afraid of other programmers abusing the new
syntax, but many of those programmers are already committing abuses as
workarounds.

Actually, I would probably write

   x = (sqrt(y) if y >= 0 else 0)

if the ternary operator were introduced, as otherwise the if reads too
much to me as if it were modifying the entire assignment, as in Perl.

Dan

-- 
http://www.dfan.org




More information about the Python-list mailing list