Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why "flat is better than nested"?

Reply
Thread Tools

Why "flat is better than nested"?

 
 
kj
Guest
Posts: n/a
 
      10-25-2010


In "The Zen of Python", one of the "maxims" is "flat is better than
nested"? Why? Can anyone give me a concrete example that illustrates
this point?

TIA!

~kj

PS: My question should not be construed as a defense for "nested".
I have no particular preference for either flat or nested; it all
depends on the situation; I would have asked the same question if
the maxim had been "nested is better than flat".
 
Reply With Quote
 
 
 
 
Alex Willmer
Guest
Posts: n/a
 
      10-25-2010
On Oct 25, 11:07*am, kj <(E-Mail Removed)> wrote:
> In "The Zen of Python", one of the "maxims" is "flat is better than
> nested"? *Why? *Can anyone give me a concrete example that illustrates
> this point?


I take this as a reference to the layout of the Python standard
library and other packages i.e. it's better to have a module hierarchy
of depth 1 or 2 and many top level items, than a depth of 5+ and only
a few top level items.

For instance

import re2
import sqlite3
import logging

import something_thirdparty

vs

import java.util.regex
import java.sql
import java.util.logging

import org.example.thirdparty.something

There are of course some Python packages that go deeper than 2
(xml.dom.minidom), but the majority are shallow. I think the
motivation is to make the packages more discoverable, and to avoid
classification arguments (does regex go under util or text). Alone the
statement would imply a single, global space ala C but that of course
is evil and so one must balance it with "Namespaces are one honking
great idea -- let's do more of those!"

I don't think it applies to data structures though. If a deeply nested
tree models your data well, then use it.

Regards, Alex
 
Reply With Quote
 
 
 
 
bruno.desthuilliers@gmail.com
Guest
Posts: n/a
 
      10-25-2010
On 25 oct, 15:34, Alex Willmer <(E-Mail Removed)> wrote:
> On Oct 25, 11:07*am, kj <(E-Mail Removed)> wrote:
>
> > In "The Zen of Python", one of the "maxims" is "flat is better than
> > nested"? *Why? *Can anyone give me a concrete example that illustrates
> > this point?

>
> I take this as a reference to the layout of the Python standard
> library and other packages i.e. it's better to have a module hierarchy
> of depth 1 or 2 and many top level items, than a depth of 5+ and only
> a few top level items.
>

(snip)

This also applies to inheritance hierarchies (which tend to be rather
flat in Python compared to most mainstreams OOPLs), as well as nested
classes etc.


 
Reply With Quote
 
Robin Becker
Guest
Posts: n/a
 
      10-25-2010
On 25/10/2010 11:07, kj wrote:
>
>
> In "The Zen of Python", one of the "maxims" is "flat is better than
> nested"? Why? Can anyone give me a concrete example that illustrates
> this point?
>

........
I believe that the following illustrates the nesting issue (I think this is from
somewhere in Chomsky)


The rat ate the corn.
The rat that the cat killed ate the corn.
The rat that the cat that the dog chased killed ate the corn.

I believe this is called central embedding.


There's also the old schoolboy saying "I know that that that that that boy said
is wrong!".

The nested nature makes the semantics quite hard. The same will be true of
nested tuple/list and similar programming structures.
--
Robin Becker

 
Reply With Quote
 
Alex Willmer
Guest
Posts: n/a
 
      10-25-2010
On Oct 25, 2:56*pm, Robin Becker <(E-Mail Removed)> wrote:
> On 25/10/2010 11:07, kj wrote:
>
> > In "The Zen of Python", one of the "maxims" is "flat is better than
> > nested"? *Why? *Can anyone give me a concrete example that illustrates
> > this point?

>
> .......
> I believe that the following illustrates the nesting issue (I think this is from
> somewhere in Chomsky)
>
> The rat ate the corn.
> The rat that the cat killed ate the corn.
> The rat that the cat that the dog chased killed ate the corn.
>
> I believe this is called central embedding.
>
> There's also the old schoolboy saying "I know that that that that that boy said
> is wrong!".
>
> The nested nature makes the semantics quite hard. The same will be true of
> nested tuple/list and similar programming structures.


I agree in the case of a suped-up hierachical record structure that
encourages code like

my_far =
the_record.something.something_else.foo[2].keep_going.bar.baz()

A tree of homogeneous nodes that one walks or recurses into (e.g. a b-
tree or r-tree) is a case where I would ignore this maxim
 
Reply With Quote
 
rantingrick
Guest
Posts: n/a
 
      10-25-2010
On Oct 25, 5:07*am, kj <(E-Mail Removed)> wrote:
> In "The Zen of Python", one of the "maxims" is "flat is better than
> nested"? *Why? *Can anyone give me a concrete example that illustrates
> this point?


Simple. This commandment (endowed by the anointed one, GvR) is
directed directly at lisp and those filthy lispers. If you don't know
what lisp is then Google it. Then try to program with it for one hour.
Very soon after your head will explode from the nested bracket plague
and then you shall be enlightened!

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      10-25-2010
In <(E-Mail Removed)> rantingrick <(E-Mail Removed)> writes:

>On Oct 25, 5:07=A0am, kj <(E-Mail Removed)> wrote:
>> In "The Zen of Python", one of the "maxims" is "flat is better than
>> nested"? =A0Why? =A0Can anyone give me a concrete example that illustrate=

>s
>> this point?


>Simple. This commandment (endowed by the anointed one, GvR) is
>directed directly at lisp and those filthy lispers. If you don't know
>what lisp is then Google it. Then try to program with it for one hour.
>Very soon after your head will explode from the nested bracket plague
>and then you shall be enlightened!


Some of the earliest programming I ever did was in Lisp. Scheme
actually. In good ol' 6.001, back in '82. I loved it. I have no
problem whatsoever with it.

Benightedly yours,

~kj
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-25-2010
On 10/25/2010 10:47 AM, rantingrick wrote:
> On Oct 25, 5:07 am, kj <(E-Mail Removed)> wrote:
>> In "The Zen of Python", one of the "maxims" is "flat is better than
>> nested"? Why? Can anyone give me a concrete example that illustrates
>> this point?

>
> Simple. This commandment (endowed by the anointed one, GvR) is
> directed directly at lisp and those filthy lispers. If you don't know
> what lisp is then Google it. Then try to program with it for one hour.
> Very soon after your head will explode from the nested bracket plague
> and then you shall be enlightened!
>

And everyone taking the Zen too seriously should remember that it was
written by Tim Peters one night during the commercial breaks between
rounds of wrestling on television. So while it can give useful guidance,
it's nether prescriptive nor a bible ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon 2011 Atlanta March 9-17 http://us.pycon.org/
See Python Video! http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-25-2010
On 10/25/2010 10:47 AM, rantingrick wrote:
> On Oct 25, 5:07 am, kj <(E-Mail Removed)> wrote:
>> In "The Zen of Python", one of the "maxims" is "flat is better than
>> nested"? Why? Can anyone give me a concrete example that illustrates
>> this point?

>
> Simple. This commandment (endowed by the anointed one, GvR) is
> directed directly at lisp and those filthy lispers. If you don't know
> what lisp is then Google it. Then try to program with it for one hour.
> Very soon after your head will explode from the nested bracket plague
> and then you shall be enlightened!
>

And everyone taking the Zen too seriously should remember that it was
written by Tim Peters one night during the commercial breaks between
rounds of wrestling on television. So while it can give useful guidance,
it's nether prescriptive nor a bible ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon 2011 Atlanta March 9-17 http://us.pycon.org/
See Python Video! http://python.mirocommunity.org/
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      10-25-2010
In <(E-Mail Removed)> Steve Holden <(E-Mail Removed)> writes:

>On 10/25/2010 10:47 AM, rantingrick wrote:
>> On Oct 25, 5:07 am, kj <(E-Mail Removed)> wrote:
>>> In "The Zen of Python", one of the "maxims" is "flat is better than
>>> nested"? Why? Can anyone give me a concrete example that illustrates
>>> this point?

>>
>> Simple. This commandment (endowed by the anointed one, GvR) is
>> directed directly at lisp and those filthy lispers. If you don't know
>> what lisp is then Google it. Then try to program with it for one hour.
>> Very soon after your head will explode from the nested bracket plague
>> and then you shall be enlightened!
>>

>And everyone taking the Zen too seriously should remember that it was
>written by Tim Peters one night during the commercial breaks between
>rounds of wrestling on television. So while it can give useful guidance,
>it's nether prescriptive nor a bible ...


Well, it's pretty *enshrined*, wouldn't you say? After all, it is
part of the standard distribution, has an easy-to-remember invocation,
etc. *Someone* must have taken it seriously enough to go through
all this bother. If it is as trivial as you suggest (and for all
I know you're absolutely right), then let's knock it off its pedestal
once and for all, and remove it from the standard distribution.

~kj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM



Advertisments