On 2009-05-29 19:08, Dino Viehland wrote:
Consider the code:
code = "def Foo():\n\n pass\n\n "
This code is malformed in that the final indentation (2 spaces) does not agree
with the previous indentation of the pass statement (4 spaces). Or maybe it's
just fine if you take the blank lines should be ignored statement from the docs
to be true. So let's look at different ways I can consume this code.
If I use compile to compile this:
compile(code, 'foo', 'single')
I get an IndentationError: unindent does not match any outer indentation level
But if I put this in a file:
f= file('indenttest.py', 'w')
f.write(code)
f.close()
import indenttest
It imports just fine.
The 'single' mode, which is used for the REPL, is a bit different than 'exec',
which is used for modules. This difference lets you insert "blank" lines of
whitespace into a function definition without exiting the definition. Ending
with a truly empty line does not cause the IndentationError, so the REPL can
successfully compile the code, signaling that the user has finished typing the
function.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com