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

 
 
John Nagle
Guest
Posts: n/a
 
      10-26-2010
On 10/25/2010 6:34 AM, Alex Willmer 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.
>
> For instance
>
> import re2
> import sqlite3
> import logging
>
> import something_thirdparty
>
> vs
>
> import java.util.regex
> import java.sql
> import java.util.logging


As in

Python 2: import urllib
Python 3: import urllib.request, urllib.parse, urllib.error

http://diveintopython3.org/porting-c...with-2to3.html

John Nagle
 
Reply With Quote
 
 
 
 
Albert Hopkins
Guest
Posts: n/a
 
      10-26-2010
On Tue, 2010-10-26 at 09:45 -0700, John Nagle wrote:
> On 10/25/2010 6:34 AM, Alex Willmer 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.
> >
> > For instance
> >
> > import re2
> > import sqlite3
> > import logging
> >
> > import something_thirdparty
> >
> > vs
> >
> > import java.util.regex
> > import java.sql
> > import java.util.logging

>
> As in
>
> Python 2: import urllib
> Python 3: import urllib.request, urllib.parse, urllib.error
>
> http://diveintopython3.org/porting-c...with-2to3.html
>


My favorite is always:

from django.contrib.auth.models import User # I know, not std lib

 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      10-26-2010
On 26/10/2010 14:18, Benjamin Kaplan wrote:
> 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.

Please, lets NOT get a newsgroup cross feed!

I don't want spam, spam, spam, python and spam in my inbox.

Ian


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

>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


Now, that's hilarious.

kj
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-26-2010
On 10/26/2010 2:44 PM, kj wrote:
> In <(E-Mail Removed)> Steve Holden <(E-Mail Removed)> writes:
>
>> 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

>
> Now, that's hilarious.
>

See, there *is* a place for humor

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
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-27-2010
On Tue, 2010-10-26, Carl Banks wrote:
> On Oct 25, 11:20*pm, Jorgen Grahn <(E-Mail Removed)> wrote:
>> On Mon, 2010-10-25, (E-Mail Removed) wrote:
>> > 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.

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


I should have mentioned that I talked about the standard C++ library:
almost no inheritance[1] and just one namespace level.

Of course you can make a layered mess of C++ if you really try[2], but
it's not something the language encourages. IMHO.

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


Right, that's one level, and you can't avoid it if you really *do* need
inheritance.

/Jorgen

[1] Not counting the black sheep, iostreams.
[2] I have seen serious C++ code trying to mimic the Java bottomless
namespace pit of despair: com::company::division:roduct::subsystem::...

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      10-27-2010
Robin Becker, 25.10.2010 15:56:
> "I know that that that that that boy said is wrong!".


What's a "that boy"?

Stefan

 
Reply With Quote
 
Robin Becker
Guest
Posts: n/a
 
      10-27-2010
On 27/10/2010 10:13, Stefan Behnel wrote:
> Robin Becker, 25.10.2010 15:56:
>> "I know that that that that that boy said is wrong!".

>
> What's a "that boy"?
>
> Stefan
>

well they say nested is hard. How about this break down


I know that X that a boy said is wrong. (any boy)
I know that X that the boy said is wrong. (a single boy)
I know that X that that boy said is wrong. (a specific boy)

now what could we substitute for X? eg "a Y", "the Y" or "that Y". Now I wonder
what Y could be eg "word" "utterance" "that".

Any way a "that boy" is a specific boy and a "that that" is a specific that etc
etc mumble mumble
-dementedly yrs-
Robin Becker

 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      10-27-2010
On 10/26/10 06:56, Steve Holden wrote:
> On 10/25/2010 3:11 PM, kj wrote:
>> 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.
>>

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


Now you know who to blame (assuming mercurial log is not lying

$ hg blame this.py
21335: s = """Gur Mra bs Clguba, ol Gvz Crgref
21337:
21335: Ornhgvshy vf orggre guna htyl.
21335: Rkcyvpvg vf orggre guna vzcyvpvg.
21335: Fvzcyr vf orggre guna pbzcyrk.
21335: Pbzcyrk vf orggre guna pbzcyvpngrq.
21335: Syng vf orggre guna arfgrq.
21335: Fcnefr vf orggre guna qrafr.
21335: Ernqnovyvgl pbhagf.
21335: Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
21335: Nygubhtu cenpgvpnyvgl orngf chevgl.
21335: Reebef fubhyq arire cnff fvyragyl.
21335: Hayrff rkcyvpvgyl fvyraprq.
21335: Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
21335: Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
21335: Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
21335: Abj vf orggre guna arire.
21335: Nygubhtu arire vf bsgra orggre guna *evtug* abj.
21335: Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
21335: Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
21335: Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
21337:
29075: d = {}
29075: for c in (65, 97):
29075: for i in range(26):
29075: d[chr(i+c)] = chr((i+13) % 26 + c)
29075:
29075: print "".join([d.get(c, c) for c in s])

$ hg log -r 21335 this.py
changeset: 21335:56190cb9ccb6
branch: trunk
user: fdrake
date: Fri Feb 08 21:13:47 2002 +0100
summary: [svn r25249] Python 10 was a success, commemorate it\!
 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      10-27-2010
In <(E-Mail Removed)> Steve Holden <(E-Mail Removed)> writes:

>On 10/26/2010 2:44 PM, kj wrote:
>> In <(E-Mail Removed)> Steve Holden <(E-Mail Removed)> writes:
>>
>>> 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

>>
>> Now, that's hilarious.
>>

>See, there *is* a place for humor


I have nothing against humor. The reason why I find "import braces"
funny is that it is so obviously a joke. But I do find it mildly
annoying (and just mildly) that a joke/hoax/farce like ZoP/this.py
is built into the standard lib, because a lot of people (not just
me) don't realize it's a joke. (In fact, the reason I learned
about ZoP/this.py was that in a reply to some post of mine in some
Python forum [maybe c.l.py], the responder simply told me to run
"import this", with the implication that it would answer whatever
it was that I was asking about. Either this person took ZoP
seriously, or was just having fun at a noob's expense. Either way,
I don't like it.) Learning a new programming language (which
entails becoming familiar not only with some new syntax, but new
libraries, new general ideas, new ways of doing stuff), is already
disorienting enough as it is. I don't see the point of making the
task any harder than it already is by injecting additional *gratuitous
confusion* in the form pseudo-rogramming advice that apparently no
experienced Python program really believes/takes seriously anyway.
I just don't understand the need of having this.py in the std lib
of all places. It's not like there's any risk of losing the ZoP
if it were removed from it. Zillions of copies of it would be
floating around in the web. But it would not be confused as
something that is somehow endorsed by those who put together the
distribution.

I know. Not a chance.

~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