Structure using whitespace vs logical whitespace

Ken Seehart ken at
Tue Dec 16 14:11:38 CET 2008

cmdrrickhunter at wrote:
> I've been trying to search through the years of Python talk to find an
> answer to this, but my Googlefu is weak.
> In most languages, I'll do something like this
> xmlWriter.BeginElement("parent");
> ----xmlWriter.BeginElement("child");
> ----------xml.Writer.Characters("subtext");
> ----xmlWriter.EndElement();
> xmlWriter.EndElement();
> Where the dashes are indentation (since some newsgroup handlers don't
> do tabs well).  XML writing is just an example.
Yes, I hate that too.  IMO Newsgroup and email clients should not remove 
> In general, I'm using indentation to show logical flow through code.
> Python's choice to give semantic meaning to whitespace prevents me
> from doing such things.  What was once reserved for logical use is now
> used syntactically.  In 90% of cases, its not needed, and whitespace
> significance seems to be pretty effective.  In that last 10%, however,
> I've been frustrated many times.
When I first learned python I was occasionally bothered by this.  Since 
then I have gotten used
to it and would not have it any other way.  I certainly would not 
consider changing the language
for this.  I consider the benefits of a uniform meaning of whitespace 
and the corresponding
consistency of indentation style, as well as the lack of punctuation, to 
be well worth the price.
Also, I have found over the years that, for reasons described below, 
that this "price" effectively
drops to zero.
> I've been using python for a few years, and gotten around this in one
> way or another, but now I want to get other who work with me to pick
> up Python.  All newbies to Python have trouble with the idea of
> whitespace sensitivity, but how can I convince them that "it just
> works better" when I have this construct which I want to use but
> can't.
I disagree with the generalization that "All newbies to Python have 
trouble with the idea...".  I
would say that perhaps most newbies that have experience with whitespace 
neutral languages
experience some initial discomfort, which is expected for any change 
from what one is used to.
I suspect that very few people who are new to programming dislike 
whitespace sensitivity.
> Has anybody found a way to emulate this behavior?  I've often done it
> by opening an expression for the whole thing, but there's a lot of
> tasks where a single expression just isn't sufficient (such as things
> with assignment).
This would depend on the specific case.  In general, if you are writing 
lots of code that contains
structure other than program control structure, you probably are missing 
an opportunity to use a
data-driven approach.

In other words, ideally the structure in your python code should be 
/only /program control structure,
in which case the indentation will be exactly where you would want it to 
be.  For that other 10%,
you probably should code your content as data (either in an external 
file or as data literals in your

The case in point is your example:


I would use a template system such as Genshi instead, so that kind of 
structure would not need
to be in my python code in the first place.

I know that this xmlWriter code is just an example, but I think that the 
principle I am describing
really does apply more or less universally.  If you are expressing 
nested structure other than
program control structure, you should be expressing your structure as data.

Here's another solution to your example that is more generally 
applicable to other situations:

content = (element, "parent", [
    (element, "child", [
        (characters, "subtext"),
        ] ),
    ] )


(Sorry if the above indentation has been removed by evil software....)

In this case I have made the code data-driven, but include the data in 
my python code.  This
means you have the extra task of implementing *do_something_with()* but 
that is usually a
trivial task, and worth the effort IMO because it makes the structure 
more readable and easier
to modify.  It also separates content from implementation, which is also 
a really good idea.  For
example, if at some point in the future I decide to use something else 
instead of *xmlWriter *to
process the data, I can do so by changing the implementation of 
> PS. In my opinion the solution would be to have the option of entering
> a "whitespace insensitive" mode which uses C style {} and ;.  The
> token to enter it could be as complicated as you want (in fact, it may
> make sense to make it complicated to discourage use unless it's really
> advantageous).  I'd sugest {{ and }} or something bigger like {={ }
> =}.  Only two problems: 1) I'm sure it would offend Guido's sense of
> language aesthetics  2) I'm sure the idea has been hashed over on this
> newsgroup to death... hence prefering a workaround instead.
A definitive "Yes" to both 1 and 2 :-)  And I wouldn't even consider my 
proposed solutions to
be "workarounds" with respect to the alleged problem of syntactical 
whitespace.  I would want
to use the same approach in C or Java simply because I prefer a 
data-driven approach where
appropriate.  Python's container literals make it particularly easy to 
express data in your code.
It just so happens that there is a high correlation between the 
temptation to use indentation for
non-programmatic structure and the appropriateness of a data-driven 

The problem with adding redundant syntactical forms (such as your 
proposed {{...}}), is that it
complicates the language.  This has two effects:

1. In order to master the language you have to learn more (mastering the 
language includes the
ability to read other peoples code as well as writing new code).

2. It produces greater variance in style based on personal preference.  
Generally, code is easier
to read when everyone uses consistent style.

One of the things that people like about python is the relative 
infrequency of special characters.
This gives python a certain flavor.  I happen to like this flavor a 
lot.  Some people don't, and I
recommend Perl to them.  But switching between two distinct dialects of 
python does not seem
like a wise idea.

I hope this helps.

Ken Seehart

More information about the Python-list mailing list