Conditional iteration

greg greg at cosc.canterbury.ac.nz
Fri Dec 15 01:16:29 EST 2006


at wrote:

> With the current Python syntax, I can create for every two lines of code a
> dozen alternative implementations:

The "one way to do it" rule seems to be widely misquoted
and misunderstood.

Of course any Turing-complete programming language is
going to provide infinitely many ways of expressing
anything.

What the "one way" rule is saying is that there is no
point in the language going out of its way to provide
two syntaxes for something with no clear reason to
prefer one or the other in any given situation. Like,
for instance, Perl having both "if (condition) statement"
and "statement if (condition)".

That's exactly the sort of thing you're proposing here,
and that's why the "one way" rule-of-thumb suggests it's
not a good idea.

It's not a hard-and-fast rule; one could argue that
list comprehensions violate it, and many people did.
Ultimately Guido decided that LCs were a big enough
win in certain situations, probably because they bring
something from the realm of statements into the realm
of expressions. Your proposal doesn't do that -- it
just rewrites a pair of statements very slightly to
give another statement, and opinions differ on whether
it would improve or hurt readability.

Furthermore, Guido has considered this exact idea
before, promoted using the same arguments, and rejected
it, so it's unlikely he would change his mind this
time around.

--
Greg



More information about the Python-list mailing list