Zen of Python

Tim Peters tim.peters at gmail.com
Wed Jan 19 22:53:41 EST 2005


[Carl Banks]
>> The gist of "Flat is better than nested" is "be as nested as
>> you have to be, no more," because being too nested is just a
>> mess.

[Timothy Fitz]
> Which I agree with, and which makes sense. However your
> "gist" is a different meaning. It's not that "Flat is better than
> nested" it's that "Too flat is bad and too flat is nested so be as
< nested (or as flat) as you have to be and no more." Perhaps Tim
> Peters is far too concise for my feeble mind <wink>

Always happy to help a fellow Tim, but I don't claim to understand the
20 Pythonic Theses, I just claim to have channelled them from Guido's
inscrutable Python-mind.

If you _were_ to ask me (and it's not something I recommend in
general), I'd say your reading "X is better than Y" as "X is always
good, and no matter how much; Y is always evil, and no matter how
little" is unreasonably literal.  For example, most would agree that
full is better than hungry, but few would take that as meaning one
should always be stuffing one's face.  And I happen to know that Guido
believes full is better than hungry, yet simultaneously sometimes
refrains from eating until he's actually hungry.  Or consider
"accurate is better than vague".  Go ahead:  just _try_ to be 100%
accurate!  For that matter, just try to be 100% flat.  Or 100%
beautiful, 100% simple, or 100% sparse.

If you were to ask for examples in Python, well, they're all over the
place, and often in what's _not_ there.  Guido is on record as saying
that Python's original simple 3-level scope rules (local, global,
builtin) were in part a reaction against the horrid abuses of deeply
nested Pascal programs he had to wrestle with.  Lexical scoping was
added only after years of debate -- and it's still usually better to
wrap state in objects (you can pass 'em around then, add useful
methods to them, display them, mutate 'em, ...).

Or note that you can't define a module inside another module, that
Python's basic list type is a flat vector instead of a
recursively-defined S-expression, and that there's no direct way to
break out of a containing loop from a nested loop.  Python encourages
flatness of various kinds in all these (and other) ways.

But that's just what I think -- nobody knows why Guido believes what
he does, least of all Guido.  Thanks to my remarkable channelling
power, at least we know *what* he believes!  I think he was grateful
to find that out too <wink>.

there's-just-no-disputing-divine-revelation-ly y'rs  - tim



More information about the Python-list mailing list