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

Chris Mellon arkanes at gmail.com
Thu Dec 15 07:47:50 EST 2005


On 15 Dec 2005 04:32:39 -0800, bonono at gmail.com <bonono at gmail.com> wrote:
>
> jmdescha... at gmail.com wrote:
> > Tolga wrote:
> > > As far as I know, Perl is known as "there are many ways to do
> > > something" and Python is known as "there is only one way". Could you
> > > please explain this? How is this possible and is it *really* a good
> > > concept?
> >
> > if you 'import this', you get a bit of Python Zen... from which I have
> > taken this line:
> > *...
> > There should be one-- and preferably only one --obvious way to do it.
> > ...*
> > If this is what you are referring to then here is an explanation.
> >
> > So there is not 'only one way', but rather there should be a way to do
> > something that is obvious, re easy to find, evident, 'given'...
> > The quality(ies) looked for here that makes this *really* good is
> > 1- that you don't spend an inordinate amount of time looking for it -
> > you just go along and use it so your mind can be focussed of what you
> > need to achieve instead of how you can achieve it.
> > 2- If it's 'obvious', then chances are others will see it and use it
> > also, so that their code is more understandable by others. For anyone
> > who has taken care of code of others this can be *really really good*
> > ;-)
> What I don't quite understand is, if it is "obvious", whether there is
> a Zen, people would still code it that way(unless of course they want
> to hide it from others or make it difficult to understand on purpose),
> there won't be any argument of "which one is the obvious way".
> And if there is an argument(or disagreement), which one is the obvious
> ?
>

Many programmers, being clever people, like to do clever things in
order to prove how clever they are. Python programmers are not immune
to this but Perl programmers absolutely revel in it.

As you may be able to tell, I place a very large value on readability
and obviousness and I despise cleverness in code and attempt to stamp
it out when I do it (although, of course, it's hard to see when you're
violating your own rules). As an example, if someone posts on this
list asking how to print the lines of a file, they'll probably get a
bunch of answers with the obvious for loop solution, and a couple from
people being clever with list comprehensions. Even the clever list
comprehension people will usually use the for loop in real code.
Asking the same question on a Perl list will result in 30 different
aswers from 20 different people, and all 30 of those will be used in
real code.

When there is one obvious way to do things, it makes for a large
degree of consistency in code. It improves the readability and
maintainability of Python code in general and flattens the learning
curve. When there is *only* one obvious way to do something, it's even
better.

By and large, programmers are smart people who enjoy solving problems.
That means that doing clever, different ways of doing things is like
candy to programmers and a real temptation. I'm no exception - I
started using perl very early in my programming career and I loved
using all the line noise and cute operators in clever ways. It took
time and some effort to drop that portion of ego from my code and
instead take pride in how mundane, straightforward, and obvious I
could make it, rather than how clever and tricky it was.

And thats why I love the "only one way to do it" thing in Python ;)

> I think it is more like there is a preferred way, by the language
> creator and those share his view.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list