Why "flat is better than nested"?

Carl Banks pavlovevidence at gmail.com
Tue Oct 26 09:27:42 CEST 2010

On Oct 25, 11:20 pm, Jorgen Grahn <grahn+n... at snipabacken.se> wrote:
> On Mon, 2010-10-25, bruno.desthuilli... at gmail.com wrote:
> > On 25 oct, 15:34, Alex Willmer <a... at moreati.org.uk> wrote:
> >> On Oct 25, 11:07 am, kj <no.em... at please.post> 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

More information about the Python-list mailing list