Why "flat is better than nested"?
Jorgen Grahn
grahn+nntp at snipabacken.se
Wed Oct 27 04:56:36 EDT 2010
On Tue, 2010-10-26, Carl Banks wrote:
> 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.
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::product::subsystem::...
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
More information about the Python-list
mailing list