Indentation problem

Skip Montanaro skip at pobox.com
Tue Mar 26 06:26:42 EST 2002


    Isaac> This is somewhat close to what I say, but a little bit off: it
    Isaac> won't reindent to the correct tab width.  E.g., if you have the
    Isaac> following

    Isaac> def f(a):
    Isaac>     print("Trying " + a)
    Isaac> if (a <= 0):
    Isaac>         return 1
    Isaac> else
    Isaac>         return a * f(a-1)
    Isaac> print("Done")

    Isaac> and you try to use the C-c > command, you end up with

    Isaac> def f(a):
    Isaac>     print("Trying " + a)
    Isaac>     if (a <= 0):
    Isaac>             return 1
    Isaac>     else
    Isaac>             return a * f(a-1)
    Isaac>     print("Done")

    Isaac> It still works, but it doesn't look nice.  What I want is a real
    Isaac> reindentation which understand how Python reads indentation,
    Isaac> making it

    Isaac> def f(a):
    Isaac>     print("Trying " + a)
    Isaac>     if (a <= 0):
    Isaac>         return 1
    Isaac>     else
    Isaac>         return a * f(a-1)
    Isaac>     print("Done")

Now that I understand better...  You are asking to transform one
syntactically correct piece of Python into another.  Starting from your
first example you can't get to your third example without some human input
on the correct nesting.  C-c > gets you to a functionally correct state
(your second chunk of code would presumably not generate a NameError and
would emit the desired output).  For small bits of code, you can just
reindent the over-indented lines with the TAB key.  For larger chunks of
code you can use the reindent.py program in the Tools/scripts directory of
the Python distribution to adjust the amount of indentation so it is
uniform.

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list