Python indentation

David Fraser davidf at sjsoft.com
Thu Jul 8 07:56:20 EDT 2004


Antoon Pardon wrote:
> Op 2004-07-08, Ville Vainio schreef <ville at spammers.com>:
> 
>>/usr/share/doc/python2.3/examples/Tools/scripts/pindent.py (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.

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

David



More information about the Python-list mailing list