Why indentation is use to denote block of code?

Miles Kaufmann milesck at umich.edu
Mon Sep 14 03:50:46 EDT 2009


On Sep 13, 2009, at 5:38 PM, AggieDan04 wrote:
> On Sep 13, 6:27 pm, Steven D'Aprano wrote:
>> On Sun, 13 Sep 2009 15:15:40 -0700, Chris Rebert wrote:
>>> In fact it's pretty much impossible to automatically indent Python  
>>> code
>>> that has had its indentation removed; it's impossible to know for  
>>> sure
>>> where the dedents should occur.
>>
>> Just like most other syntactic elements -- if you remove all the  
>> return
>> statements from Python code, or dot operators, it's impossible to
>> automatically add them back in.
>>
>> The only difference is that some (badly written?) applications mangle
>> leading whitespace, but very few feel free to remove other text on  
>> a whim.
>>
>> I don't recall actually using a mail client or newsreader that  
>> removes
>> leading whitespace when posting, but I've occasionally seen posts  
>> from
>> others with all indentation removed, so presumably such badly-behaved
>> applications do exist.
>
> I haven't seen it in a mail client, but it's very common in internet
> forums.

If you regularly deal with some sort of transport that messes with  
your leading whitespace, you may find Tools/scripts/pindent.py in the  
Python source distribution useful; it adds comments that act as block  
closers to your code, and can then use those comments to restore the  
correct indentation to a mangled version.  (Most forums offer some  
sort of whitespace-preserving [code] tag, though; and pindent is  
relatively old, and apparently not well maintained (no support for  
"with" blocks)).

-Miles




More information about the Python-list mailing list