Question about indentation and bugs

Peter Hansen peter at
Fri Aug 24 07:08:13 CEST 2001

Dave Swegen wrote:
> However, I (and others here at work) do have some real concerns about
> the indentation feature. Basically we are quite worried about subtle
> but nasty bugs creeping in. Take the following code for instance:
> for i in [1234]:
>         print i
>         print "hello"
> Perfectly fine, valid code. But one indentation step wrong, and you end
> up with perfectly fine, valid code that does something else, ie
> for i in [1234]:
>         print i
> print hello

On the other hand, with certain other languages which use 
braces sometimes but not always, you can get a similar effect:

for (i = 1234; i <= 1234; i++)
    printf("%d\n", i);

Those dang braces just don't help if you don't use 'em. :-)

> I imagine that these sort of problems might not only appear due to
> incorrect coding, but also things such as broken mailers, applying
> patches from people using different indent levels etc

Broken mailers are, well, broken, and I don't think many of
us pay them much attention.  We also probably make sure we
don't send real code unpackaged as text very often, so the
issue doesn't arise.  

As far as people using different indent levels, this one also 
rarely arises.  Python, more than any other language I recall
seeing, has a consistent coding style across a very large
portion of its user base.  That is very likely helped along
largely by

> So my question (or rather questions) is this: In practice, how often do
> such problems crop up?  How does one go about avoiding such problems? Is
> there any way at all to make explicit where a block ends?

*In practice*, this is rare enough to be acceptable.  Proper
education of programmers using Python makes its occurrence
effectively negligible.  It does happen that somebody tries
to edit some code with an editor which displays tabs as
four characters (a Very Bad Thing), but this is usually 
caught quite quickly and fixed.  If you have a relatively
consistent coding style in the first place, you will NOT
find this to be an issue.

Maybe more importantly, if you don't actually use TABs, 
or you consistently treat TABs as eight spaces, the problem 
won't arise.  I think most Python programmers tend towards 
spaces _only_, and that mostly avoids any problems 
_in practice_. 

What may well happen is you will find after a brief time
coding in Python that you find it so refreshingly readable
and easy to type, that you will quickly start to look with
extreme distaste on code which actually _requires_ explicit
block begin/end markers.  Python's vastly higher readability
more than makes up for the marginal increased risk of problems
caused by indentation mismatches.

Peter Hansen, P.Eng.
peter at

More information about the Python-list mailing list