473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Zen of Python

While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

Flat is better than nested.

I strive for balance between flat and nested. Does anyone have a good
example of where this is applied? (specifically to python, or in
general)
Jul 18 '05 #1
6 1742
Timothy Fitz wrote:
While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

Flat is better than nested.

I strive for balance between flat and nested. Does anyone have a good
example of where this is applied? (specifically to python, or in
general)


Such sayings are deliberately somewhat ambiguous or simplified,
partly because they make one think a little more that way,
partly because the PSU places strict limits on how many characters
are allowed in Pythonic sayings. (Some PSU members are very small
and the output of "import this" didn't fit on their T-shirts without
this restriction.) (You never read this message. These are not
the droids you're looking for.)

Regardless, think of the meaning of "flat is better than nested"
as being in essence what you say you strive for. Comparing extremes
-- "completely flat is better than incredibly deeply nested" --
is sort of pointless. It seems likely, therefore, that nobody should
infer from this that completely "flat" is always better than any
amount of nesting, no matter how little.

And with that out of the way, one is left with "there's a balance
along the flat/nested dimension which is appropriate to any
given situation, so nest with moderation and only when necessary".

But that didn't fit in the list so they went with the shorter version...

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Timothy Fitz wrote:
While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

Flat is better than nested.
[incrdeibly secret PSU facts blurted out]
And with that out of the way, one is left with "there's a balance
along the flat/nested dimension which is appropriate to any
given situation, so nest with moderation and only when necessary".
They'd probably been talking to Antoon Pardon :-)
But that didn't fit in the list so they went with the shorter version...

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #3

Timothy Fitz wrote:
While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

Flat is better than nested.

I strive for balance between flat and nested. Does anyone have a good
example of where this is applied? (specifically to python, or in
general)

I think the essence of why this Zen is true is the recognition that the
world isn't really organized into a nice, proper, perfect heirarchy,
where every node is a perfect little subset of its parent.

The fact is, some things are flat. A list is flat. Is is not better
to deal with flat things flatly?

And some things aren't flat, but they're not nested either. Rather,
they are various overlapping sets, but not subsets. It it better to
deal with such as mess by shoehorning it into a heirarchy that isn't
applicable, or to make it flat and deal with the subsets individually?

I shall give two examples of where Python exhibits this Zen, and doing
one my favorite things in the process: slamming other languages.
ITERATION

There are two ways to iterate: the flat way, with a for loop or list
comprehension or something like that; and the nested way, recursively.
We all know that recursion is often absolutely necessary. But usually
flat iteration suffices.

In other languages (notoriously LISP and functional languages), there
is a tendency to do it recursively anyways. For example, the following
Python code illustrates a recursive way to copy a list that would be
considered an example of "good code" in a language such as LISP:

.. def copylist(a):
.. if a: return a[:1] + copylist(a[1:])
.. else: return a

LISP, of course, was designed to make recursive processing like this
easy and efficient. But it doesn't, IMHO, keep recursion from being
much harder to figure out. Although this has a sort of coolness in
that you can see the list copy operation reduced to its simplest
possible form (in the same way that Game of Life is cool because you
get all kinds of complexity from very simple rules), it completely
misses the big picture. When I want to copy a list, I want to copy a
list; I don't want to figure out the minimal rule set I could use to do
it. Iteration fits the mind better.

Python, IMO, wisely put the focus on iteration.
POLYMORPHISM

In many languages, the only way to get dynamic polymorphism is to
subclass. If two classes are to share an interface, they have to both
be subclasses of a common base class. If you have lots of classes that
you want to share parts of their interfaces, you have to put them all
into that heirarchy.

The problem is, the world isn't a neatly organized tree, where every
type of object must have functionality that is an exact proper subset
of some other type of object. So what you end up with is this big,
messy, inflexible hierarchy of classes that is difficult to make
changes or add new options to. Many classes have stuff in them that
ought not to be there, just to satisfy the requirements of subclassing.
Oftentimes, the root class has lots of methods that many subclasses
don't implement. Oftentimes, there will be subclasses with
functionality that isn't polymorphic because the root class doesn't
define virtual methods for them.

(For an example of all this madness: in I/O hierarchies, the base class
often has seek() and tell() methods, but since not all streams are
seekable, it also has to have a seekable() method. Thus, polymorphic
behavior and its benefits are abandoned in favor of the procedural way.
Sure, for this case, you could just define a SeekableStream
intermediate class, only for seekable streams. But here's the thing:
there are any number of functionalities a stream may or may not have.
Are you going to design a hierarchy with branches to account for all
possible combinations of them?)

Not so in Python. In Python, if you want to classes to have the same
interface, then write two classes that have the same methods. Bam, you
got polymorphism. No shoehorning into a heirarchy necessary. It's
what we call duck typing. (If it looks like a duck, and floats like a
duck, it's made of wood.)

The flat method of polymorphism is so much better it isn't even funny.
Again, Python chose wisely.
--
CARL BANKS

Jul 18 '05 #4
On Wed, 19 Jan 2005 14:13:47 -0500, Timothy Fitz <fi******@gmail.com>
wrote:
While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

(snip)

For those interested, here's the list:

http://www.python.org/doc/Humor.html#zen

Luke.
Jul 18 '05 #5
Luke Skywalker <lu**@tatooine.planet> schreef:
On Wed, 19 Jan 2005 14:13:47 -0500, Timothy Fitz
While I agree that the Zen of Python is an amazingly concise list
of truisms, I do not see any meaning in:


For those interested, here's the list:
http://www.python.org/doc/Humor.html#zen


Or you just type at the interactive prompt:
import this


Regards,
Jan

Jul 18 '05 #6
Op 2005-01-19, Steve Holden schreef <st***@holdenweb.com>:
Peter Hansen wrote:
Timothy Fitz wrote:
While I agree that the Zen of Python is an amazingly concise list of
truisms, I do not see any meaning in:

Flat is better than nested.

[incrdeibly secret PSU facts blurted out]

And with that out of the way, one is left with "there's a balance
along the flat/nested dimension which is appropriate to any
given situation, so nest with moderation and only when necessary".

They'd probably been talking to Antoon Pardon :-)


Just what I thought: They'll be accusing me of this :-)

--
Antoon Pardon
Jul 18 '05 #7

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

Similar topics

0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.