[Python-Dev] Conditional For Statements (original) (raw)

Alex Martelli aleaxit at gmail.com
Mon May 19 16:57:33 CEST 2008


On Mon, May 19, 2008 at 5:33 AM, Steven D'Aprano <steve at pearwood.info> wrote: ...

import this The Zen of Python, by Tim Peters ... There should be one-- and preferably only one --obvious way to do it. There should be ONE OBVIOUS way to do it, not "only one way".

The "only one way" phrasing is in the "Spirit of C" section of the ISO C Standard `Rationale' (non-normative, but highly informative), specifically in point four, "provide only one way to do an operation". I contend that the somewhat different phrasing is somewhat accidental and not particularly relevant: the intent of this language design guideline is exactly the same (Tim was writing on his own, while the C Standard was written by a committee, so it's hardly surprising that the Standard's wording is drier and less funny than Tim's;-). IOW, I've often argued that this is a deep commonality between the design philosophy of C and Python (together with "trust the programmer" and "keep the language small and simple", two more points in the "Spirit of C" section) which makes them conceptually "closer" to each other than C is to languages sharing more of its syntax, from C++ to Javascript; it should be particularly easy to see the contrast with languages deliberately designed to provide multiple equivalent synonyms (such as foo.size and foo.length meaning the same thing in Ruby) or going out of their way to provide multiple equivalent syntax approaches for exactly the same semantics (such as Cobol's "ADD A TO B GIVING C" and "C = A + B", or SQL's explicit [INNER] JOIN form and the equivalent form where the inner join is left implicit in the WHERE clause).

A design guideline is not a strict rule -- e.g., every C programmer has met situations where a while statement, and a for statement with some of the three subparts in the control clause omitted, can both make sense, and of course every time you write within your loop some "if(condition) continue" you wonder if in this case it might not be better to instead use "if(!condition) { ... }" around the rest of the loop's body, etc, etc -- so much for "only one way", as the various ways to perform elementary operations can click together in a large number of combinations and permutations; and adding "obvious" would not help much either, since both alternatives can easily happen to be "equally obvious". Python is just in the same situation. Nevertheless, keeping the guideline firmly in mind (e.g., eschewing synonyms in APIs you design that provide no added value beyond that of catering to different tastes or to a meta-taste for variety over uniformity;-) is still a helpful heuristic -- with or without the "obvious" word (i.e., whether you're working with C, which doesn't have the "obvious" in its phrasing of the principle, or with Python, which does).

Alex



More information about the Python-Dev mailing list