PEP 308: Pep Update

Anders J. Munch andersjm at dancontrol.dk
Thu Feb 27 13:10:06 EST 2003


"Sean Ross" <sross at connectmail.carleton.ca> wrote:
> 
> "Lulu of the Lotus-Eaters" <mertz at gnosis.cx> wrote in message
> news:mailman.1046332734.23329.python-.
> >
> > In every Python construct that exists now, a condition is stated BEFORE
> > the actions that depend on it.
> >
> 
> Hi.
> "Contrariwise", this is not true in 'every' Python construct:
> 
>     seq = range(100)
>     evensquares = [x*x for x in seq if not x%2]
> 
> So, 'The ternary form "x if C else y" [does not break] this uniformity',
> list comprehensions do.

Yes.  And that makes list comprehensions the most confusing Python
construct there is.  Not only doesn't it read left to right, it
doesn't even read right to left either.

Until I tried it, I fully expected

    [x*y for x in (1,2) for y in (-1,+1)]

to produce

    [-1, -2, 1, 2]

which would follow from consistent right-to-left semantics.

Now since I use list comp.s a lot, I've gotten used to it and it
doesn't bother me anymore.  It's ok for commonly-used syntax to be
special, because once learned it isn't forgotten.

But the rarer a construct is, the more conventional and boring its
syntax and semantics should be - even at the cost of terneness and
elegance.

> 
> 
> +1 "x if C else y"
> 

Had I expected conditional expressions to be used a lot, I might have
liked that too.

- Anders






More information about the Python-list mailing list