Indentation and errors
Back to the python machine on the desk. Before printing anything, it looks over the whole sheet: are the lines written correctly? If the sheet has a line that the rules do not allow, the machine hands the sheet back and prints nothing. Even the correct lines above the error stay unprinted.
Let's look at such a sheet. The second line has an unclosed parenthesis and the first one is correct, yet it prints nothing either:
print("one")
print("two" # ✗SyntaxError: '(' was never closedThe interpreter first checks the whole file against the rules of writing code, and only then runs it. It reports a SyntaxError found during the check before running the first line, so one does not appear.
Interviewers like to ask this: why did an error in the last line keep the first one from running? The answer: before the run, the interpreter parses the whole file. It finds a broken rule of writing before it runs a single line.
Indentation and blocks
Indentation is the spaces at the start of a line, and in Python they are part of the rules of writing code. Indentation shows which lines form a block—a group of lines that belong to one line above them.
A block has two rules:
- it can start only after a line that ends in a colon;
- its lines are shifted to the right, by convention 4 spaces.
Programs made only of print calls have no lines with a colon, so every line starts at the first character. The interpreter finds an extra indent during the same check of the file and reports an IndentationError.
An error while running
The check of the file does not notice a typo in a function name: the rules of writing are not broken. The machine accepts the sheet and starts printing.
print("one") # one
print("two") # two
pritn("three") # ✗
print("four")NameError: name 'pritn' is not defined. Did you mean: 'print'?Let's see what happens when it runs:
- The first line prints
one. - The second line prints
two. - On the third line the interpreter looks for the function
pritn, does not find it and stops the program with aNameError. - The fourth line no longer runs, and
fourdoes not appear.
Every error that only running can find behaves this way. The lines above it have already run, and their output stays on the ribbon.
How to read a traceback
The error message takes several lines and is called a traceback. For the error in the example above it looks like this:
Traceback (most recent call last):is the header: the error happened while running.File "…/hello.py", line 3, in <module>gives the path to the file and the line number of the error.- The line
pritn("three")itself, with^marks under it that point at the place of the error. NameError: name 'pritn' is not defined. Did you mean: 'print'?gives the kind of error and the reason.
Read a traceback from its last line: it tells you right away what happened. The line number from the second line of the traceback shows where to look.
Errors found during the check of the file have no Traceback line. But their last line is built the same way: the kind of error, a colon and the reason.
Common mistake
A space has slipped in before a call:
print("one")
print("two") # ✗IndentationError: unexpected indentOne space is enough: the line above has no colon and opens no block. So the indent on the second line is extra. The check of the file finds the error, so not even one is printed. The fix is to delete the spaces at the start of the line.
Key points
- Before the run, the interpreter checks the whole file, and an error of writing—a
SyntaxErroror anIndentationError—lets no line run. - An indent opens a block, and a block can start only after a line with a colon.
- An error while running, such as a
NameError, stops the program at its line, and the output of the lines above is already printed. - A traceback is read from its last line: it holds the kind of error and the reason.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.