2

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 ?

asked Jul 8, 2012 at 11:34
4
  • 5
    I don't understand get your question - you're answering it yourself. (i.e. using indentation.) Commented Jul 8, 2012 at 11:43
  • i think my asnwer is clear"in c family we show block of code using { and }...but in python we dont use this character but how python can recognize block of code?? Commented Jul 8, 2012 at 15:33
  • 1
    The same way you do. By seeing how the code is indented. In my mind, this is one advantage of Python. The python interpreter recognizes code blocks in the exact same way the developer does. (Well...assuming your editor converts tabs to spaces.) Commented Jul 8, 2012 at 21:49
  • ok...so brief answer is "python use tab to recognize block"....nice! Commented Jul 9, 2012 at 7:29

4 Answers 4

10

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.

answered Jul 8, 2012 at 19:41
7

Remember that whitespace appears as just another character to the interpreter.

  • A tab is 0x09 in ASCII
  • A space is 0x20 in ASCII

An interpreter is little more than a pattern matcher that then triggers predefined rules.

answered Jul 8, 2012 at 11:54
2

At the parser level, it's not all that difficult. The parser just needs a variable to keep track of the indentation level of the current block. And when it goes to read a new line, it counts leading spaces (or tabs) and compares the value against the indentation of the current block, then apply an algorithm that looks something like this:

if currentIndent = currentBlock.indent then
 parse line in the context of currentBlock
else if currentIndent > currentBlock.indent then
 create sub-block of currentBlock and parse line in that context
else finish currentBlock and run this same comparison on currentBlock.parent
answered Jul 8, 2012 at 12:40
3
  • There should also be a special case for blank lines - Python skips those. Commented Jul 8, 2012 at 13:21
  • There is also a special case for whitespace-ignoring contexts; for example, argument lists. Commented Jul 8, 2012 at 15:23
  • 1
    Actually, for pretty much all existing Python implementations, the parser works exactly like it would in an ALGOL-like language, because the lexer keeps track of the indentation and injects fake BLOCK_BEGIN and BLOCK_END tokens into the token stream. IOW: the parser is kept mostly context-free by not making the lexer regular. Commented Jul 9, 2012 at 1:44
1
  • A block begins when the indentation increases.
  • A block ends when the indentation decreases.
  • Multiple blocks can be closed in the same decrease.
  • Blocks can nest.
  • Sequential blocks at the same indentation depth join up.
answered Jul 8, 2012 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.