python idioms : some are confusing

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 21 02:23:57 EDT 2012


On Thu, 20 Sep 2012 22:34:48 -0700, Vineet wrote:

> Amongst the python idioms, how the below-mentioned make sense?

They're not Python idioms. Idioms are common pieces of code, like looping:

for item in sequence:
    do_something

What you have quoted are parts of the Zen of Python, which is 
deliberately named. Zen koans are notorious for being contradictory and 
impossible to understand.

As Terry Pratchett wrote:

    In the second scroll of Wen the Eternally Surprised a story
    is written concerning one day when the apprentice Clodpool, 
    in a rebellious mood, approached Wen and spake thusly: 
    "Master, what is the difference between a humanistic, monastic
    system of belief in which wisdom is sought by means of an 
    apparently nonsensical system of questions and answers, and a 
    lot of mystic gibberish made up on the spur of the moment?" 
    Wen considered this for some time, and at last said: "A fish!" 
    And Clodpool went away, satisfied.
    -- (Terry Pratchett, Thief of Time)


So be careful about over-interpreting the Zen of Python. Half of it is 
meant to followed seriously, half is meant as a joke, and half is meant 
as a guideline only.


> ## There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.

This tells us that for any task you might want to do in Python, there 
should be some way to do it which is obvious. It is not enough that there 
is some (hard to find, convoluted) way to do it, it should be obvious. 
And while it isn't forbidden to be two or more obvious ways, it is better 
if there is only one.

The joke is that even this single sentence goes against its own advice. 
There are at least three obvious ways to put a parenthetical aside in a 
sentence:

There should be one--and preferably only one--obvious way to do it.
There should be one -- and preferably only one -- obvious way to do it.
There should be one (and preferably only one) obvious way to do it.

The author of the Zen deliberately choose a fourth, non-obvious way.

Finally, the second line warns that although Python has many obvious ways 
to solve things, they may only be obvious to the creator of Python, Guido 
van Rossum, who is Dutch.


> --- In programming, there can be a number of ways, equally efficient, to
> do certain  thing.

The Zen refers to the philosophy that Python the language should provide 
an obvious way to solve a problem. The emphasis is on the *obvious* part, 
not the *one* part.


> ## Although never is often better than *right* now.
> 
> --- How come "never" is better that "right now" ?


Solving a problem in the language -- adding a new language feature such 
as a keyword, new syntax, a library, etc. -- should only be done when 
that new feature brings more benefit than problems. But sometimes a new 
feature might bring more problems than benefits. In this case, it is 
better to *never* solve that problem *in the language* than to add a 
feature that solves the problem badly and causes more problems than it 
solves. E.g. multi-line lambdas.

The problem is that once you add a feature to the language, it becomes 
almost impossible to remove it. You are stuck with it nearly forever, or 
at least for many years. So better to not add it than to be stuck with a 
bad feature.



-- 
Steven



More information about the Python-list mailing list