Python indentation

David Fraser davidf at
Fri Jul 9 12:07:33 CEST 2004

Antoon Pardon wrote:
> Op 2004-07-08, David Fraser schreef <davidf at>:
>>Antoon Pardon wrote:
>>>Op 2004-07-08, Ville Vainio schreef <ville at>:
>>>>/usr/share/doc/python2.3/examples/Tools/scripts/ (Debian)
>>>>  Antoon> This is why I prefer free form languages. If you need
>>>>  Antoon> certain control structure that is not directly in the
>>>>  Antoon> language but can be simulated, you can still indent your
>>>>  Antoon> code according to the structure you have in mind as
>>>>  Antoon> opposed to the structure that is forced upon you by the
>>>>  Antoon> simulation.
>>>>You realize that this approach is slightly heretical, do you?
>>>>Indentation should follow the "real" block structure *exactly*,
>>>>anything else is an error that confuses the reader. 
>>>Why should the indentation follow the block structure instead of
>>>structure of the algorithm
>>>>The while 1 - break structure doesn't even need extra clarification,
>>>>because the break is typically in a very idiomatic place - right after
>>>>the assignment in the beginning, or at the very end if it's
>>>>semantically a repeat-until loop (which are rare).
>>>I have loops that don't conform to the above description. Like
>>>the following.
>>>    loop
>>>      shift = 0
>>>      delta = 1
>>>      full = 1L
>>>      while (self.bits & full) == 0:
>>>        full = (full << delta) + full
>>>        shift = delta
>>>        delta = delta * 2
>>>    breakif shift == 0:
>>>      self.offset = self.offset + shift
>>>      self.bits = self.bits >> shift
>>>And this is just a simple example. Indented like I did here,
>>>clearly shows where the loopbreaker occurs, which is less
>>>clear when loopbreaker is indented the same way as the rest
>>>of the loop.
>>>So why should I be forced to indent in a way that doesn't
>>>reflect the structure of the algorithm, simply because
>>>the language is not rich enough.
>>The fact that the block structure doesn't match the structure of the 
>>algorithm is what you are finding a problem here. What you really want 
>>is a different loop construction.
> Well that would of course be the best solution. But in absence 
> of a more general loop construction I'm willing to settle for
> a construction that looks like it, even if it has to be
> simulated.

The thing is, the readability is lost if what it looks like is different 
to what it actually does. Anyway, you can't do that using Python because 
of the indentation of the language so if you want to you'll have to use 
another language :-)

>>But I personally find the above code hard to read - it takes a lot of 
>>thinking to try and work out what you are trying to do here whichever 
>>way you indent it. I think your indentation actually confuses the issue more
> Maybe you are just not familiar with more general loop constructs.
> What the algorithm is trying to do is find the least significant bit
> that is on. It does so by applying succeeding masks to the number
> each mask is all ones and double in length as the previous mask.
> when applying a mask results in a number different from 0, the
> number is shifted the length of the previous mask and the length
> is added to the offset. The process is then repeated with the
> mask initialized at 1. The algorthm stops when applying a mask
> of 1 already differs from 0.

Sure, but I didn't find the loop structure itself readable. Maybe just a 
  matter of what some people like others don't.

More information about the Python-list mailing list