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.