Attitude about new keywords

Peter Hansen peter at engcorp.com
Mon Feb 10 13:05:05 EST 2003


Michele Simionato wrote:
> 
> I guess this is subjective, I am not bothered at all by the reverse
> order in list comprehensions
> 
> [x for x in range(10) if x!=5]
> 
> which I find very readable. Nevertheless, I see from the PEP thread that
> many don't like that. Mah! misteries of the human nature...

Actually, to me this makes perfect sense.  Of _course_ the if should
go after the rest of it, since if you put it first you wouldn't have
introduced the name "x" yet.

In the case of the list comprehension, the conditional is a minor
part of the overall structure.

In the case of the ternary expression, however, the condition
is *the* key item.  Putting the true result first, *then*
the condition, and then the false result, is just plain backwards.
No different than the "do all this long stuff if test" syntax
from Perl.

Just because the test in the list comprehension comes later is
no reason to insist that it must come later in other cases.  
Foolish consistency is ... oh, never mind.  Someone else already
said that. :-)

-Peter




More information about the Python-list mailing list