Why "flat is better than nested"?

Discussion in 'Python' started by kj, Oct 25, 2010.

  1. kj

    kj Guest

    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".
     
    kj, Oct 25, 2010
    #1
    1. Advertising

  2. kj

    Alex Willmer Guest

    On Oct 25, 11:07 am, 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 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
     
    Alex Willmer, Oct 25, 2010
    #2
    1. Advertising

  3. kj

    Guest

    On 25 oct, 15:34, Alex Willmer <> wrote:
    > On Oct 25, 11:07 am, 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 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.
     
    , Oct 25, 2010
    #3
  4. kj

    Robin Becker Guest

    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
     
    Robin Becker, Oct 25, 2010
    #4
  5. kj

    Alex Willmer Guest

    On Oct 25, 2:56 pm, Robin Becker <> 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
     
    Alex Willmer, Oct 25, 2010
    #5
  6. kj

    rantingrick Guest

    On Oct 25, 5:07 am, 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?


    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!
     
    rantingrick, Oct 25, 2010
    #6
  7. kj

    kj Guest

    In <> rantingrick <> writes:

    >On Oct 25, 5:07=A0am, kj <> 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
     
    kj, Oct 25, 2010
    #7
  8. kj

    Steve Holden Guest

    On 10/25/2010 10:47 AM, rantingrick wrote:
    > On Oct 25, 5:07 am, 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?

    >
    > 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/
     
    Steve Holden, Oct 25, 2010
    #8
  9. kj

    Steve Holden Guest

    On 10/25/2010 10:47 AM, rantingrick wrote:
    > On Oct 25, 5:07 am, 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?

    >
    > 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/
     
    Steve Holden, Oct 25, 2010
    #9
  10. kj

    kj Guest

    In <> Steve Holden <> writes:

    >On 10/25/2010 10:47 AM, rantingrick wrote:
    >> On Oct 25, 5:07 am, 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?

    >>
    >> 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
     
    kj, Oct 25, 2010
    #10
  11. kj

    Terry Reedy Guest

    On 10/25/2010 3:11 PM, kj wrote:

    > Well, it's pretty *enshrined*, wouldn't you say?


    No.

    > After all, it is part of the standard distribution,


    So is 'import antigravity'

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


    If you are being serious, you are being too serious (as in humorless).
    The 'Zen of Python' is somewhere between 'trivial' and 'enshrined on a
    pedestal'. The two undocumented easter egg imports (visible in
    pythonx.y/Lib) are for both fun and edification.

    --
    Terry Jan Reedy
     
    Terry Reedy, Oct 25, 2010
    #11
  12. kj

    Steve Holden Guest

    On 10/25/2010 3:11 PM, kj wrote:
    > In <> Steve Holden <> writes:
    >
    >> On 10/25/2010 10:47 AM, rantingrick wrote:
    >>> On Oct 25, 5:07 am, 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?
    >>>
    >>> 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.
    >

    I don't know who decided to put the "this" module into Python as an
    Easter egg. But don't think you can suppress it now. Trying to do so
    would only bring out people's inherent religious fervor and cause an
    outcry you would regret.

    Besides which I am sure Tim Peters derives a lot of harmless fun from
    seeing people take it so seriously.

    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/
     
    Steve Holden, Oct 25, 2010
    #12
  13. kj

    Ethan Furman Guest

    kj wrote:
    > In <> Steve Holden <> writes:
    >
    >>> On Oct 25, 5:07 am, 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?


    Two points on the practical side: most folk only remember a few levels
    deep, so shallow is easier to work with*; and, while premature
    optimization is usually a waste of time, effort, money, hair, etc., each
    level costs another lookup.

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


    The Zen is good humor, and good advice. An excellent reminder to strive
    for balance in all things...

    ~Ethan~

    --
    * citation needed, I know
     
    Ethan Furman, Oct 25, 2010
    #13
  14. kj

    Jorgen Grahn Guest

    On Mon, 2010-10-25, wrote:
    > On 25 oct, 15:34, Alex Willmer <> wrote:
    >> On Oct 25, 11:07 am, 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 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.


    Which mainstream languages are you thinking of? Java? Because C++ is
    as flat as Python.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 26, 2010
    #14
  15. kj

    Carl Banks Guest

    On Oct 25, 11:20 pm, Jorgen Grahn <> wrote:
    > On Mon, 2010-10-25, wrote:
    > > On 25 oct, 15:34, Alex Willmer <> wrote:
    > >> On Oct 25, 11:07 am, 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 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.

    >
    > Which mainstream languages are you thinking of?  Java?  Because C++ is
    > as flat as Python.


    Not in my experience. The only way to get dynamic polymorphism (as
    opposed to the static polymorphism you get with templates) in C++ is
    to use inheritance, so when you have a class library in C++ you tend
    to get hierarchies where classes with all kinds of abstract base
    classes so that types can be polymorphic. In Python you don't need
    abstract base classes so libraries tend to be flatter, only inheriting
    when behavior is shared.

    However it's not really that big of a difference.


    Carl Banks
     
    Carl Banks, Oct 26, 2010
    #15
  16. kj

    kj Guest

    In <> Terry Reedy <> writes:

    >On 10/25/2010 3:11 PM, kj wrote:


    >> Well, it's pretty *enshrined*, wouldn't you say?


    >No.


    > > After all, it is part of the standard distribution,


    >So is 'import antigravity'


    Are you playing with my feelings?

    % python
    Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    >>> import antigravity

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named antigravity

    Too bad, I was looking forward to that.

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


    >If you are being serious, you are being too serious (as in humorless).


    Guilty as charged, both in the "too serious" and the "humorless"
    counts. :/ Blame it on the Asperger's.

    My only defense is that, while learning Python over the past year,
    I've had *many* "you've got to be joking" moments while reading
    what's ostensible "serious" Python documents (e.g. PEP 8, PEP 257)
    as well as assorted threads featuring GvR and others involved in
    the design of Python, to the point that sometimes I do have a hard
    time gauging the seriousness of what's considered "good programming"
    / "best practice" in the Python world.

    Plus, I think it's fair to say that the Python community as a whole
    (or at least its more vocal members) are more concerned with
    "correctness" (for lack of a better term) and "code aesthetics"
    than, say, the Perl community. E.g., only in Python-related threads
    I've seen the adjective "icky" used routinely to indicate that some
    code is unacceptable on (more or less) aesthetic grounds.

    My point is that, even if one detects some levity in ZoP, given
    everything else one runs into in the Python world, for the uninitiated
    like me it is still hard to distinguish between what's in jest and
    what's in earnest.

    Perhaps the disconnect here is that you're seeing the whole thing
    from an insider's point of view, while I'm still enough of an
    outsider not to share this point of view. (I happen to think that
    one the hallmarks of being an initiate to a discipline is an almost
    complete loss of any memory of what that discipline looked like
    when the person was a complete novice. If this is true, then it's
    easy to understand the difference in our perceptions.)

    Anyway, thanks for letting me in on the joke. I'll pass it on.

    (Though, humorless as it is of me, I still would prefer the ZoP
    out of the standard library, to save myself having to tell those
    who are even newer to Python than me not to take it seriously.)

    ~kj
     
    kj, Oct 26, 2010
    #16
  17. On Tue, Oct 26, 2010 at 9:05 AM, kj <> wrote:
    > In <> Terry Reedy <> writes:
    >
    >>On 10/25/2010 3:11 PM, kj wrote:

    >
    >>> Well, it's pretty *enshrined*, wouldn't you say?

    >
    >>No.

    >
    >> >  After all, it is part of the standard distribution,

    >
    >>So is 'import antigravity'

    >
    > Are you playing with my feelings?
    >
    > % python
    > Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
    > [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    >>>> import antigravity

    > Traceback (most recent call last):
    >  File "<stdin>", line 1, in <module>
    > ImportError: No module named antigravity
    >
    > Too bad, I was looking forward to that.
    >


    Try it in Python 3.

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

    >
    >>If you are being serious, you are being too serious (as in humorless).

    >
    > Guilty as charged, both in the "too serious" and the "humorless"
    > counts. :/  Blame it on the Asperger's.
    >
    > My only defense is that, while learning Python over the past year,
    > I've had *many* "you've got to be joking" moments while reading
    > what's ostensible "serious" Python documents (e.g. PEP 8, PEP 257)
    > as well as assorted threads featuring GvR and others involved in
    > the design of Python, to the point that sometimes I do have a hard
    > time gauging the seriousness of what's considered "good programming"
    > / "best practice" in the Python world.
    >


    This is a programming language named after a British comedy group (not
    the snake). There are going to be jokes inserted in lots of otherwise
    serious things. Like the standard library.


    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Benjamin Kaplan, Oct 26, 2010
    #17
  18. kj

    Seebs Guest

    On 2010-10-26, kj <> wrote:
    > (Though, humorless as it is of me, I still would prefer the ZoP
    > out of the standard library, to save myself having to tell those
    > who are even newer to Python than me not to take it seriously.)


    Well, not to take it *too* seriously.

    It's like any other Zen -- it's wonderful as long as you take it about
    the right amount seriously. If someone could tell you how seriously
    that is, it wouldn't be Zen.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 26, 2010
    #18
  19. kj

    Steve Holden Guest

    On 10/26/2010 9:05 AM, kj wrote:
    > Perhaps the disconnect here is that you're seeing the whole thing
    > from an insider's point of view, while I'm still enough of an
    > outsider not to share this point of view. (I happen to think that
    > one the hallmarks of being an initiate to a discipline is an almost
    > complete loss of any memory of what that discipline looked like
    > when the person was a complete novice. If this is true, then it's
    > easy to understand the difference in our perceptions.)
    >

    That can be true of most technical communities, and Python is no
    exception. As someone who does quite a lot of training the challenge is
    always to hold on to those outsider perceptions to avoid the learners
    feeling lost.

    > Anyway, thanks for letting me in on the joke. I'll pass it on.
    >
    > (Though, humorless as it is of me, I still would prefer the ZoP
    > out of the standard library, to save myself having to tell those
    > who are even newer to Python than me not to take it seriously.)


    The answer is probably the same as you will see if you try

    from __future__ import braces

    That feature *is* available in Python 2.6 ;-)

    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/
     
    Steve Holden, Oct 26, 2010
    #19
  20. kj

    Robin Becker Guest

    On 26/10/2010 15:42, Steve Holden wrote:
    > he answer is probably the same as you will see if you try
    >
    > from __future__ import braces
    >
    > That feature*is* available in Python 2.6;-)

    In the past I used to think it was really cool that one could do

    from __future__ import exciting_and_cool_new_stuff


    now I really wish we had

    from __past__ import old_and_boring_syntax_26

    etc etc
    -trapped on level five-ly yrs-
    Robin Becker
     
    Robin Becker, Oct 26, 2010
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  3. Terry Reedy

    Why 'Flat is better than nested'

    Terry Reedy, Jul 31, 2012, in forum: Python
    Replies:
    0
    Views:
    161
    Terry Reedy
    Jul 31, 2012
  4. Ian Kelly

    Re: Why 'Flat is better than nested'

    Ian Kelly, Jul 31, 2012, in forum: Python
    Replies:
    0
    Views:
    149
    Ian Kelly
    Jul 31, 2012
  5. Terry Reedy

    Re: Why 'Flat is better than nested'

    Terry Reedy, Aug 1, 2012, in forum: Python
    Replies:
    0
    Views:
    152
    Terry Reedy
    Aug 1, 2012
Loading...

Share This Page