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

Chris Mellon arkanes at gmail.com
Thu Dec 15 08:29:00 EST 2005


On 15 Dec 2005 05:08:02 -0800, bonono at gmail.com <bonono at gmail.com> wrote:
>
> Chris Mellon wrote:
> > 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 ;)
> >
> It is perfectly ok to define coding policy within an organisation, for
> a project that have more than one developer and things like that. But
> if the language allows more than one way to do it, people would try if
> that is what they want and they can.

You can never stop people from being clever. You can only encourage
them to improve themselves. It's like when a teenager finally realizes
that exactly how many studs he has on his jacket really isn't that
important. (Am I dating myself? Do teenagers still put studs on their
jackets?)

>
> I would say that if "only one way to do it" is the intend, make it into
> the language and any other way is simply error.

These are called declarative languages and people hate them because
they aren't flexible or extensible. There is a big, big difference
between "only one way to do something, by fiat" and "only one obvious
way to do something".

>Say if ternary operator
>is not the "preferred way", don't have it in the language. If someone
> find a way to work around it, change that part of the language to break
> their code.

My understanding of the PEP involved is that the ternary operator is
coming into Python not because of it's merits, but because of the
overly-clever and often broken work-arounds people insist upon using.
It's better to have one clear, obvious way to do something (if/else on
one line, in this case) than it is to have a bunch of often-broken
attempts. You seem very, very interested in portraying anyone who
wants to encourage good style and readability as a language Nazi. I
don't appreciate that. You'll notice that I haven't taken the easy way
out and told you to go away and play with Perl, right?

There are some parallels with writing prose. Mark Twain is a
fantastic, very skilled author by any ones standards but he wrote in a
very straightforward, accessible manner. There's a quote of his to the
effect of "kill your darlings". Write a chapter or whatever, then
re-read it and take out everything you're really proud of, because
those are the bits where you've indulged yourself at the expense of
your reader.

Same philosophy in code. Everyone knows you're smart. You don't have
anything to prove by writing clever code. It's like putting a spoiler
on the back of your Hyundai - you aren't impressing anyone. It is
actually much harder to write simple code, if only because you need to
exert the discipline to resist the impulse to be clever. Python
encourages simplicity, obviousness, and consistency. Those are
*enormous* virtues in every facet of programming. There is never a
benefit to being clever.

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?

>
> --


> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list