473,732 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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?

Dec 15 '05 #1
44 4204

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

Jean-Marc

Dec 15 '05 #2

jmdescha...@gma il.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
?

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

Dec 15 '05 #3
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?

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 comprehensibili ty,
as primary values. Readability, comprehensibili ty and a welcoming
approach to newcomers ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 15 '05 #4
On 15 Dec 2005 04:32:39 -0800, bo****@gmail.co m <bo****@gmail.c om> wrote:

jmdescha...@gma il.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

Dec 15 '05 #5

Chris Mellon wrote:
On 15 Dec 2005 04:32:39 -0800, bo****@gmail.co m <bo****@gmail.c om> wrote:

jmdescha...@gma il.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.

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.

Dec 15 '05 #6
On 15 Dec 2005 05:08:02 -0800, bo****@gmail.co m <bo****@gmail.c om> wrote:

Chris Mellon wrote:
On 15 Dec 2005 04:32:39 -0800, bo****@gmail.co m <bo****@gmail.c om> wrote:

jmdescha...@gma il.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 todo
> 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

Dec 15 '05 #7
bo****@gmail.co m wrote:
[...]
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 15 '05 #8

Chris Mellon wrote:
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.

Dec 15 '05 #9
On 15 Dec 2005 05:08:02 -0800
bo****@gmail.co m wrote:
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".
--
Terry Hancock (ha*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Dec 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
6995
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or maybe even just declare an interface, with no methods implemented). Right now, you don't really have a way to do it. You can leave the methods with a "pass", or raise a NotImplementedError, but even in the best solution that I know of,
6
7066
by: Mason A. Clark | last post by:
LAST WORD(s): 1. MSIE6 and Firefox will go to the top of the page on command <a href="#top">go upsy</a> even if there is NO name="top" or id="top" They know what a "top" is :-) Opera does not.
11
4219
by: Paul D.Smith | last post by:
Can Python create a variable "on-the-fly". For example I would like something like... make_variable('OSCAR', 'the grouch'); print OSCAR; ....to output... the grouch
3
1370
by: Petr Prikryl | last post by:
Hi all, My question is: How do you tackle with mixing Unicode and non-Unicode parts of your application? Context: ======== The PEP 3000 says "Make all strings be Unicode, and have a separate bytes() type."
1
4328
by: Mark E. Hamilton | last post by:
Sorry, I probably should have re-stated the problem: We're using Python 2.3.5 on AIX 5.2, and get the follow error messages from some of our code. I haven't yet tracked down exactly where it's coming from: sem_trywait: Permission denied sem_wait: Permission denied sem_post: Permission denied
10
4701
by: schears | last post by:
Why? Running on windows 2000 with all updates, 2G Memory, 117G Hard Drive space available. This was not an issue until I added some code to two of my c files. Any suggestions? Thanks
4
3838
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is absolutely no added functionality to it.
10
2931
by: RDI | last post by:
What's it mean? My prog runs fine. Then as soon as I press ok or cancel, the following is what's in the output area of the debugger. TIA -- RDI (remove the exclamation from the email address)
2
2624
by: John Nagle | last post by:
For some reason, Python's parser for "robots.txt" files doesn't like Wikipedia's "robots.txt" file: False The Wikipedia robots.txt file passes robots.txt validation, and it doesn't disallow unknown browsers. But the Python parser doesn't see it that way. No matter what user agent or URL is specified; for that robots.txt file, the only answer is "False". It's failing in Python 2.4 on Windows and 2.5 on Fedora Core.
56
6737
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under 6.4.2(2) : case constant-expression : I propose that the case expression of the switch statement be changed from "integral constant-expression" to "integral expression".
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.