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

T

Tolga

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?
 
J

jmdeschamps

Tolga said:
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*
;-)

Jean-Marc
 
S

Steve Holden

Tolga said:
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?
Perl's credo is actually "There's more than one way to do it", often
abbreviated to TMTOWTDI.

(Part of) Python's credo (which you can read in context by typing

import this

at an interactive command prompt) is "There should be one (and
preferably only one) way to do it".

Clearly as Python is improved (well, develops, anyway :) new ways to
perform old tasks will become possible. So the obvious way to iterate
over the lines of a text file *used* to be

while 1:
line = f.readline()
if not f:
break
# process the line

whereas now it's

for line in f:
# process the line

As with all credos this should be approached with a large-ish grain of
salt and a pragmatic air. As with all Zen knowledge it's important to
avoid taking the Zen too literally, otherwise it may be necessary to hit
you on the side of the head with a stick to get you moving back towards
enlightenment <0.8 wink>.

We try not to be rude to perlmongers on this group. After all, they have
come much closer to world domination than we have so far, and they can't
help their peculiar penchant for coding in what looks like line noise.
[As you can see it's OK to poke a bit of good-humoured fun at them from
time to time].

Overall Python emphasises two things, readability and comprehensibility,
as primary values. Readability, comprehensibility and a welcoming
approach to newcomers ...

regards
Steve
 
B

bonono

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
?

I think it is more like there is a preferred way, by the language
creator and those share his view.
 
C

Chris Mellon

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 ;)
 
B

bonono

Chris said:
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.

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. 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.
 
C

Chris Mellon

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?
 
S

Steve Holden

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.

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. 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.
This would have the unfortunate side effect of only allowing changes to
Python that allowed users to do things which are currently impossible.

Since Python is Turing-complete, this would effectively inhibit all
further changes to the language.

Would you, say, remove "for" loops because they could be written as
"while" loops. Don't forget the word "obvious" that appears in that
catchphrase ...

regards
Steve
 
B

bonono

Chris said:
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?
Noop. My stand is that good style and readability is a subjective
matter, to certain extend. The often quoted example of the ternary
operator demonstate that. I have no problem reading it as "this is the
preferred way in python, because we have done a large usability study
which indicate that", but saying it as "obvious" as if it is truth is
just strange to me and yes I would repeatedly pointing that out. You
can tell me to go away but that won't change anything. For most people,
they are not what you described as "trying to be smart" but because of
their background, truly feel that their way is more intuitive(or
obvious). It is just like there are language on this planet that reads
from right to left horizontally, as well as top to bottom, then right
to left. And you are trying to tell them that English way is the "right
way" or the obvious way.

I can't get your meaning of "language Nazi" so using the idiom of
"don't assume", I cannot comment on that part.
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.
I don't know where you get that idea.

For most of the question I see asking here, they are from another
language background and their obvious way is what they are asking for
that they cannot find in python.
 
T

Terry Hancock

On 15 Dec 2005 05:08:02 -0800
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. 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.

But that is precisely what it does mean -- Python's language
design tries to be "reasonably minimal": there's usually one
fairly easy way to do a task. Unintentionally, there may
well be a half-dozen really hard ways to do it. The point of
telling this to the potential coder is to suggest that "if
it's hard, you're probably doing it the wrong way" and nudge
them into looking at how the language designers have
intended those problems to be solved.

But of course, this is simply a reaction to the Perl motto:
Having a half-dozen more or less equally difficult, equally
documented methods, with no expressed preference, means that
you will find all half-dozen in the wild, which means in
turn that reading and understanding code is six-times harder
to do. Since readability is the primary use of source code,
this translates to the language being pretty close
to six-times harder to use overall.

The problem that has happened with Python is that as the
language is improved, the old "best way" gets replaced with
a new "best way" (hopefully easier). The Python development
team's response to this problem is to specifically
deprecate the old way, and encourage users to adopt the new
way. Generally, the old way does still work so that code
isn't needlessly broken, though there have been exceptions
(such as when the scoping rules were changed).

OTOH, no one can possibly anticipate everything you need to
do, and it is always possible that you have important
requirements for doing things a certain way. We assume that
you are mature enough to know the difference between
"breaking the rules accidentally" and "breaking the rules on
purpose".
 
B

bonono

Steve said:
This would have the unfortunate side effect of only allowing changes to
Python that allowed users to do things which are currently impossible.

Since Python is Turing-complete, this would effectively inhibit all
further changes to the language.
I don't quote understand the above.
Would you, say, remove "for" loops because they could be written as
"while" loops. Don't forget the word "obvious" that appears in that
catchphrase ...
If every "for" usage can be done with "while" and that "while" is the
preferred way, why not ? As I said, the problem is that "obvious"
really is subjective in many case. And if it really is obvious, it
really is obvious and I doubt there would be that much disagreement. I
am a left handed and any time I use something that is designed to be
right-handed, I have the same feeling too, luckily there isn't that
much thing in real life.
 
B

bonono

Terry said:
But that is precisely what it does mean -- Python's language
design tries to be "reasonably minimal": there's usually one
fairly easy way to do a task. Unintentionally, there may
well be a half-dozen really hard ways to do it. The point of
telling this to the potential coder is to suggest that "if
it's hard, you're probably doing it the wrong way" and nudge
them into looking at how the language designers have
intended those problems to be solved.
Um, that is fine. However, what I usually see is like this :

C-programmer learning python :

Hi, where is condition ? true : false

someone prefer the if/else statement type:

Can't you see that the following is much more readable, stupid(well not
the exact word but tone in such a way like words of messy or elegant
etc.)

if condition:
true
else:
false
 
S

Steve Holden

Chris Mellon wrote:
[...]
(Am I dating myself? ...)
Do we need to know about your love life here? Are you hermaphroditic? If
not the relationship will never go anywhere.

regards
Steve
 
B

Ben Sizer

Steve said:
Would you, say, remove "for" loops because they could be written as
"while" loops. Don't forget the word "obvious" that appears in that
catchphrase ...

Interestingly - and somewhat related to this - the other day I was
looking for a do..while or do..until loop in the Python language
reference, thinking that there must be a statement for it, since
semantically they're distinct from while loops. I had a use case that
could have been slightly simplified by such a construct. The fact that
I didn't find one seemed slightly strange at first, coming from a
C/Pascal background, although it did occur to me that I've used Python
for years now and not noticed this lack before.
 
S

Steve Holden

I don't quote understand the above.
It says that Python is already adequately expressive to allow it to
solve all solvable problems: more briefly, "Python can already do
everything". Hence there is no need to change the language.

Of course I use this as a /reductio ad absurdum/ to try to show you the
falsehood of your position. Sadly I fear this will simply result in
another response which won't move the dialogue forwards.
If every "for" usage can be done with "while" and that "while" is the
preferred way, why not ? As I said, the problem is that "obvious"
really is subjective in many case. And if it really is obvious, it
really is obvious and I doubt there would be that much disagreement. I

It seems to me you either don't understand the words "obvious" and
"preferably".
am a left handed and any time I use something that is designed to be
right-handed, I have the same feeling too, luckily there isn't that
much thing in real life.
I believe I have also suggested that the phrases of the Zen aren't to be
taken too literally. You seem to distinguish between "obvious" meaning
"obvious to Steve but not necessarily to me" and "really obvious"
meaning "obvious to both Steve and me". So where does the subjectivity
creep in? And are you going to spend the rest of your life arguing
trivial semantics?

regards
Steve
 
J

jmdeschamps

Yes, a shared preferred way.
And the same is true of many... Think Haskell, OCaml, Lua, Ruby, Lisp,
Smalltalk, Java, C... They all have qualities of some sort, that appeal
to some of us. Not all the same, nor to all of us. It's really a
question of perspective.

In this Python community, one shared preferred way is obviousness, that
we agree or disagree upon, that we argue or ignore as best suits us. It
is not 'the law', it is a wish. For clarity, for communication, for
exchange and for certainly many more reasons.

As a personal experience, I've often been able to use the language
structures, and functions for that matter, out of thin air, because it
seemed *obvious* in the context. But the Help files remain close-by...

Jean-Marc
 
G

Gerard Flanagan

Tolga said:
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?

Yes it is a good concept because you can concentrate on Strategy rather
than Tactics.

Strategy and Tactics

in warfare, related terms referring, respectively, to large-scale and
small-scale planning to achieve military success. Strategy may be
defined as the general scheme of the conduct of a war, tactics as the
planning of means to achieve strategic objectives. Not all theorists of
war make this a primary distinction. In the Chinese and Japanese
traditions processes and paradoxes are emphasized more than categories
(see Sun Tzu ). Karl von Clausewitz , the Prussian military theorist,
who was influenced by Niccolo Machiavelli , described strategy as the
planning of a whole campaign and tactics as the planning of a single
battle. In Clausewitz's theory all military strategy is part of the
larger political pattern, and all the nation's resources are to be
subordinated to the task of attaining the political objective of the
war; to this concerted effort he gave the name "grand strategy."
Antoine H. Jomini , an influential Swiss military theorist and general,
regarded strategy as the art of moving forces to the field of battle
and tactics as the conduct of forces in battle. Another school views
strategy as a means of bringing the enemy to battle and tactics as the
means of defeating him in battle. Some theorists focus on clear sets of
general principles; some wrote books on principles, formations and
maneuvers; and still others dwell on the importance of spirit or other
intangibles.
taken from http://www.encyclopedia.com/html/s1/strategy.asp


Sun Tzu
The Art of War.....stresses the unpredictability of battle, the
importance of deception and surprise, the close relationship between
politics and military policy, and the high costs of war. The futility
of seeking hard and fast rules and the subtle paradoxes of success are
major themes. The best battle, Sun Tzu says, is the battle that is won
without being fought.
from http://www.encyclopedia.com/html/S/SunT1zu.asp


Gerard
 
S

Steve Holden

Ben said:
Steve Holden wrote:




Interestingly - and somewhat related to this - the other day I was
looking for a do..while or do..until loop in the Python language
reference, thinking that there must be a statement for it, since
semantically they're distinct from while loops. I had a use case that
could have been slightly simplified by such a construct. The fact that
I didn't find one seemed slightly strange at first, coming from a
C/Pascal background, although it did occur to me that I've used Python
for years now and not noticed this lack before.
You'll find it's exercised the group frequently from time to time.
Without wishing to stir the whole thing up again, the essence of the
problem is the unnatural fit with Python's suite design.

Would you say

do:
suite
while condition

or what? Basically do ... while and do ... until most naturally put the
test after the loop body (suite), and it's difficult to think of a
consistent and natural syntax for expressing the construct. Not that
this stopped lots of people from coming forward with their personal
favourites ... some suggestions even offered "n and a half" looping
possibilities.

In the end nobody managed to convince Guido that a suitable solution was
readily to hand, so nothing happened.

regards
Steve
 
B

bonono

Steve said:
This would have the unfortunate side effect of only allowing changes to
Python that allowed users to do things which are currently impossible.

Since Python is Turing-complete, this would effectively inhibit all
further changes to the language.
I don't quote understand the above.
Would you, say, remove "for" loops because they could be written as
"while" loops. Don't forget the word "obvious" that appears in that
catchphrase ...
If every "for" usage can be done with "while" and that "while" is the
preferred way, why not ? As I said, the problem is that "obvious"
really is subjective in many case. And if it really is obvious, it
really is obvious and I doubt there would be that much disagreement. I
am a left handed and any time I use something that is designed to be
right-handed, I have the same feeling too, luckily there isn't that
much thing in real life.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,054
Latest member
Ruby74713

Latest Threads

Top