Q: What does "Sparse is better than dense" mean? (Python Zen)

Peter Hansen peter at engcorp.com
Thu Jul 11 23:31:07 EDT 2002


jepler at unpythonic.net wrote:
> 
> On Thu, Jul 11, 2002 at 04:30:59PM -0700, Andrae Muys wrote:
> > Well seeing as we are discussing Python Zen, the one I don't currently
> > understand is "Flat is better than Nested".  I had always thought the
> 
> Nested is
>     A
>         B
>             C
>                 D
> 
> flat is
>     A
>         B
>         C
>         D
> 
> Python has lots of namespaces, but they're relatively flat.  Usually,
> module->class->function or module->function, but with nested_scopes
> sometimes an additional level of function is seen.
> 
> That's my take on it ...

If that's true (and it sounds good to me) then one might interpret
the advice as "don't nest for the sake of nesting; nest only to 
manage complexity".

It is not necessary to nest to manage three items.  It might be
necessary to nest a little more to manage twenty or thirty.
Several hundred or thousand need a little more (i.e. the standard
library, which does have a few levels of nesting, such as package
xml).

?

-Peter



More information about the Python-list mailing list