0

I noticed in prototyping some code that (a script that is analogous to) the following works

a = 1
exit()
for i in range(2):
    some # undefined variable

but this does not

a = 1
exit()

    some # undefined variable

The latter results in IndentationError: unexpected indent.

Whilst not essential to work, in quickly prototyping my code and isolating bugs, I frequently put in early exits. I was surprised that the indentation error after the exit() is picked up by the interpreter but other errors (e.g. referencing undefined variables) are not.

What accounts for this behavior and why was this design choice made by the Python core developers? Is there a way I can run my syntactically-correct-until-the-exit script without fixing indentation errors after the exit() first.

Signposting to a relevant textbook chapter or PEP in addition to the explanation of the behavior would be great.

Anil
  • 1,097
  • 7
  • 20
  • Python does need to compile your code to its own form of bytecode. A compile-time error (like incorrect indentation, which confuses the compiler) will stop it from doing that in advance of when the interpreter actually gets to the line in question. – Green Cloak Guy Nov 01 '22 at 18:49
  • 1
    An undefined variable may be ok if the code doesn't branch into it, BUT you can't run it at all if the indentation is broken – azro Nov 01 '22 at 18:50
  • 1
    One is a script execution error, the other is a parsing error. Related: https://stackoverflow.com/questions/42746745/does-executing-a-python-script-load-it-into-memory – jarmod Nov 01 '22 at 18:50
  • 1
    because indentation errors are compile-time errors in Python. They happen when the source code is parsed. Name errors are runtime errors. – juanpa.arrivillaga Nov 01 '22 at 18:53
  • 1
    It's not picked up by the interpreter. It's picked up by the parser - which makes sense if you think about it: Your source code has to be parsed before it can be executed, because you can only execute a Python script that is syntactically correct. If you feed your parser some text that looks like Python source code for the most part, but isn't actually Python source code (because it has a syntax error), then it cannot be executed. – Paul M. Nov 01 '22 at 18:57
  • If you are running a checker (like `pylama`) in your editor, e.g. when the file is saved, that *would* pick up on the undefined name. – Roland Smith Nov 01 '22 at 19:05
  • Note that you can do this by having a line of triple-quotes before and after your notations. `"""` / `Python doesn't care about this.` / `"""` – Tim Roberts Nov 02 '22 at 00:27

1 Answers1

2

Python compiles your code to an intermediate representation. Your question is fair: there are languages that don't do this. For instance, this is a perfectly runnable Tcl snippet, despite the second line being absolute nonsense

exit 0
])} total garbage line of C0De ))))

But Python works differently. It reads the whole file into an intermediate representation and then runs it. So if the code can make sense in some context, then it'll compile but it may not run. But if the code is absolutely, unequivocally invalid Python, then it won't parse.

If you're hacking together a script and want to get rid of some code quick, then the usual convention is to wrap it in triple quotes.

a = 0

'''
   nonsense code I haven't
   finished writing goes here
'''

with proper indentation, this treats the offending code as a constant string literal, which will be silently created then discarded at runtime.

Alternatively, some editors have a quick way of adding # to the start of lines. For example, in Emacs I can use the comment-line command, bound to C-x C-; by default to automatically add or remove # signs from the start of lines.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116