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

Greg Brunet gbrunet at nospamsempersoft.com
Sat Feb 8 03:09:09 EST 2003


"Andrew Koenig" <ark at research.att.com> wrote in message
news:yu994r7fhmnh.fsf at europa.research.att.com...
> Jamie> Little annoyances like that add up, and make code harder to
> Jamie> read.  I like the fact that Python keeps things simple and
> Jamie> consistent, and I'm perfectly happy to trade the conciseness
> Jamie> offered by this proposed syntax for the clarity of doing it
> Jamie> "the long way",
:
:
:
> Some people would write something like this:
>
>         strs = []
>         for i in ints:
>             if i < 0:
>                 strs.append("?")
>             else:
>                 strs.append(str(i))
>
> but others (including me) would like to be able to write this:
>
>         strs = [("?" if i < 0 else str(i)) for i in ints]
>
> I find this example easier to understand than the previous one.
>
> I think that if you like list comprehensions and lambda expressions,
> you'll probably like PEP 308; if you don't, you probably won't.

There was a recent thread "Myth: Python is ideal for beginners" where a
number of folks seemed to take offense at the idea that Python was NOT
easy to pick up.  I consider myself a pretty experienced programmer - I
know lots of different languages (though not Perl which is evidently the
origin of some of these constructs), and the first time I ran into
lamda's and list expressions, I did a triple take trying to figure out
what was happening.  In fact, as I recall, it took quite a bit of work
to figure out what it was I should be looking up in the help files:
where's the operator or keyword that I should be searching under.  Now I
realize that ease of learning and ease of use are not necessarily
compatible, but, even though I can switch between an RPN and algebraic
calculator with ease, I struggle with parsing LC's, lambdas, and even to
a degree this newly proposed if-then-else syntax easily.  I'm not
proposing that Python should become COBOL - verbosity is no guarantee of
readability either, but this seems to take Python further down the road
of a write-only language.

BTW: In my first (and second) reading of the 2 code snippets above, I
missed the ( ) around the if-then-else, so I was reading it that the if
i<0 test ran first, and only if it's false did the 'for'
statement/clause run.  Now you can certainly argue that I was being
careless in reading the code, but is the savings of a few lines of code
really worth the potential for confusion when another developer looks at
and has to support the code?


-- 
Greg





More information about the Python-list mailing list