Indentation problem
Duncan Booth
duncan at NOSPAMrcp.co.uk
Mon Mar 25 09:49:08 EST 2002
Gilles Diribarne <gdiribarne at udcast.com> wrote in
news:mailman.1017062259.25098.python-list at python.org:
> EXAMPLE:
>
> pythona.py:
> def func():
> a = 1
> if a == 1:
> a ==2
>
>
>
> pythonb.py
> def func2():
> a = 2
> if a == 2:
> print "Yeah!"
>
>
> You cannot merge correctly or tell me how to do this ?
>
Sadly, neither pythona.py nor pythonb.py is syntactically valid. To make
them acceptable to Python, you must remove all the indentation before the
'def' keywords. Once you do this, so that each function begins with a line
that is not indented your problem goes away. Just copy and paste the code,
and even though the function bodies are indented differently everything
will work.
In practice copying Python code is less usual than in many other languages
because Python makes it so much easier to reuse code in-place without
copying. Just remember to put the body of any script in a block with "if
__name__=='__main__':" guarding it, and you can then safely import the
script and pick out any functions you need at runtime.
Code reuse beats copying.
BTW, Python does have a block end marker. It is spelled '# end'. Look in
your Python installation for the file Tools/Scripts/pindent.py. Running
this program with the -c option will 'correct' your code to use the block
end marker everywhere. The -d option will make your code readable to
everyone else again. If you mess up the indentation then the -r option will
use the #end markers to put indentation back in again.
See also reindent.py in the same directory for making source files use
consistent indentation.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list