Code reformater?

Gabriel Genellina gagsl-py at
Sat Jan 20 22:38:00 EST 2007

At Sunday 21/1/2007 00:15, Steven D'Aprano wrote:

>On Sat, 20 Jan 2007 23:51:24 -0300, Gabriel Genellina wrote:
> > As the indentation *is* significant in python, none of the above can
> > help if you lose the indentation. Try to reconstruct this:
> >
> > def foo():
> > if a>0:
> > if b>0:
> > print 1
> > print 2
> > else:
> > return 3
> > return 4
> >
> > The tools may help to make the indentation consistent (tabs/8
> > spaces/4 spaces/2 spaces mixed) or look better, but not to make it right.
>Sure -- but a heuristic that gets it right *sometimes* may still be
>useful, provided the user knows that the code may not be indented
>There are lots of legal Python blocks where the indentation CAN be
>reconstructed correctly, and only a relatively small proportion where it
>can't --

I'd say absolutely the opposite. Try the example above. Or this one, simpler:

def foo():
if a>0:
if b>0:
print 1
print 2

Which level should be 'print 2' assigned to? There are 3 choices. 
What if there is a third print? There are 3, 2, or 1 possibilities, 
depending on where you put the previous one. The same for any other 
statement below.
The problem is, after a colon, you know that a new block begins and 
you must indent, but you don't know when that block ends except in a 
few cases (an else clause, by example, and only if there was a single 
preceding if).

>the tool could do its best, and warn the user when there are
>indents that can't be dealt with. Or even refuse the temptation to guess,
>but re-indent whatever parts of the code it is sure about.

...almost nothing, I'm afraid... :(

>Still, it is better not to lose the indentation in the first place.


Gabriel Genellina
Softlab SRL 


Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the Python-list mailing list