Why and how "there is only one way to do something"?

Rocco Moretti roccomoretti at hotpop.com
Thu Dec 15 10:32:44 EST 2005


bonono at gmail.com wrote:
> Chris Mellon wrote:
> 
>>Any time you want to write something in any way other than the obvious
>>way, ask yourself why? Is it more obvious *to you*, which is a good
>>reason as long as you're only writing code for yourself? Or is it just
>>to be different, or because you think it'll be faster, or just because
>>the slickness of it appeals to you?
>>
> 
> The point is again, "obvious" is not so obvious sometimes. You seem to
> be assuming that anyone that use style different from you is
> intentionally doing it and that your style would first come to their
> mind but they don't use it, for the sake of proving that they are
> smart.

My take on it is that "obvious" is intended to be prescriptive, not 
descriptive. (Note that in the Zen it is phrased "There *should* be 
...".) It describes what Python aspires to, not what it is. If the 
currently preferred method is not "the one obvious way", steps should be 
taken to make the preferred way "the obvious way" (i.e. the way that you 
reach for first, when you want to "do it").

Keep in mind that the Zen was written at a time when there was a certain 
amount of "Perl vs. Python deathmatch" feeling in the air. That line in 
the Zen was a reaction to Perl's "There's more than one way to do it 
(TMTOWTDI)."

Perl took the view that flexibility was a virtue to be praised above all 
others, and allows and encourages (or at least used to) different ways 
of doing things. I don't think a syntactic construct was excluded from 
Perl for the sole reason "well, we already can do that with this 
construct ..."

Python, in part due to a backlash to the Perl philosophy, emphasized 
clarity and readability. There are *many* constructs which have been 
excluded from Python just because they weren't any clearer than what is 
already in the language. (Once a language is "Turing complete", anything 
you can program a computer to do, you can use that language to do. There 
is no guarantee that it's short or pretty, though.) That's what the "one 
obvious way" refers to - the clearest, most readable way of expressing 
what you want to accomplish.

In this "obviousness", there is (often) little consideration for what 
other languages you previously might have used, or might be trying to 
drag idioms from. As an absurd analogy, if you were born and grew up in 
a country with a crazed totalitarian leader who, under penalty of death, 
made you turn around three times while hopping and whistling the 
national anthem before you sat down for dinner, it might be "obvious" to 
you that one must turn around three times, hopping and whistling before 
sitting down for dinner. However, if you move out of that country to, 
say, the Netherlands, for instance, you no longer need to 
hop-whistle-turn, and your new countrymen will look at you strangely if 
you do. That's not to say you can't hop-whistle-turn if the fancy 
strikes you, but in any practical setting, people will expect you do the 
simple, "obvious" thing -- just sit.


BTW. People who are quick to bring up the Zen (and I'm as guilty as 
others at times) should also keep in mind that the Zen is found under 
the *humor* section of the Python website. It's not an actual design 
document, it's just a surprisingly intuitive description of Python's nature.



More information about the Python-list mailing list