Actually these are "fake" bloc delimiters (in the shape of comments, see example in the original post).

By this I mean they are used by the formatting tool (pindent) only, not by the language (Python itself).

They are (generated by and used by) pindent for the sake of being able to fix the indent level in python code when :
  1. A copy / paste went bad (e.g. the last line of a for bloc has been "pasted at the wrong indentation level").
  2. A source file lost all indentation when been mailed because, say, the tabs have been stripped
  3. etc...

I do not see how there can be an equivalent of gofmt if there is no *indication* of the end of the blocs (independent of the indentation, that is).


It is my feeling that without such a tool Python is inherently very vulnerable to glitches occurring at editing time:
    1. Copy / paste glitch that passes unnoticed, does not generate an exception but alters the logic of the program.
    2. Tab key inadvertently hit.
    3. Difficulty in assessing the target indentation level when a part of a bloc has to be pasted in a different part of the code.

Serge Hulne.



On Thu, May 26, 2011 at 7:29 AM, Serge Hulne <serge.hulne@gmail.com> wrote:
Suggestion: Integrate the script "pindent.py" as standard command for formatting pyhton code

Here is the link;
http://svn.python.org/projects/python/trunk/Tools/scripts/pindent.py

Pindent stands for "Pyton indent":

Goal :
  1. It provides bloc delimiters (end of blocks) in the for of comments (like "#end if" or "#end for" etc ... )
  2. This allows one to check / restore the indentation of Python code, in cases where>
    1. A copy/paste went wrong
    2. The indentation of a Python source got corrupted when the script was posted on web page, send via email etc ...
    3. Standardise (fix) sources which happily mix whitespaces and tabs
    4. Make Python code more readable for developers used to end of blocs delimiters (Ruby, C, C++, C#,Java, etc ...)
 Basically the idea is the same as the Go language "gofmt" (Go format).

Example:

#-------------------
- Before using pindent:

#!/usr/bin env python

i = 0
for c in "hello world":
    if c == 'l':
        i+=1
        print "number of occurrences of `l` :", i

#------------------
- After using indent:

#!/usr/bin env python

i = 0
for c in "hello world":
    if c == 'l':
        i+=1
        print "number of occurrences of `l` :", i
    # end if
# end for


Serge Hulne