How Indentation is Parsed
Whitespace is handled by the lexical analyzer before being parsed.
The lexical analyzer uses a stack to store indentation levels. At the beginning, the stack contains just the value 0, which is the leftmost position. Whenever a nested block begins, the new indentation level is pushed on the stack, and an "INDENT" token is inserted into the token stream which is passed to the parser. There can never be more than one "INDENT" token in a row (
When a line is encountered with a smaller indentation level, values are popped from the stack until a value is on top which is equal to the new indentation level (if none is found, a syntax error occurs). For each value popped, a "DEDENT" token is generated. Obviously, there can be multiple "DEDENT" tokens in a row.
The lexical analyzer skips empty lines (those containing only whitespace and possibly comments), and will never generate either "INDENT" or "DEDENT" tokens for them.
At the end of the source code, "DEDENT" tokens are generated for each indentation level left on the stack, until just the 0 is left.
is analyzed as:
The parser than handles the "INDENT" and "DEDENT" tokens as block delimiters.
The spacing should be even and uniform throughout. Improper indentation can cause an
IndentationError or cause the program to do something unexpected. The following example raises an
Or if the line following a colon is not indented, an
IndentationError will also be raised:
If you add indentation where it doesn't belong, an
IndentationError will be raised:
If you forget to un-indent functionality could be lost. In this example
None is returned instead of the expected
For Python, Guido van Rossum based the grouping of statements on indentation. The reasons for this are explained in the first section of the "Design and History Python FAQ". Colons,
:, are used to declare an indented code block, such as the following example:
Spaces or Tabs?
The recommended indentation is 4 spaces but tabs or spaces can be used so long as they are consistent. Do not mix tabs and spaces in Python as this will cause an error in Python 3 and can causes errors in Python 2.