Python – How Python Interpreter Recognizes Code Blocks

interpreterslanguage-designpython

The most unusual aspect of Python is that whitespace is significant
instead of block delimiters (braces → "{}" in the C family of languages), indentation is used to indicate where blocks begin and end.
how can python interpreter recognize code block ?

Best Answer

The Python documentation, in the Lexical Analysis section, describes briefly how the indentation parsing works. In short, the tokeniser generates special INDENT and DEDENT tokens that are used by the parser when deciding where blocks of code start and end. These tokens (roughly) correspond to the { and } tokens in C-like languages.

Related Topic