Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question
5  
I don't understand get your question - you're answering it yourself. (i.e. using indentation.) –  Mat Jul 8 '12 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?? –  Jeson Park Jul 8 '12 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.) –  Steven Burnap Jul 8 '12 at 21:49
    
ok...so brief answer is "python use tab to recognize block"....nice! –  Jeson Park Jul 9 '12 at 7:29
add comment

4 Answers

up vote 11 down vote accepted

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.

share|improve this answer
add comment
  • 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.
share|improve this answer
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
    
There should also be a special case for blank lines - Python skips those. –  btilly Jul 8 '12 at 13:21
    
There is also a special case for whitespace-ignoring contexts; for example, argument lists. –  syrion Jul 8 '12 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. –  Jörg W Mittag Jul 9 '12 at 1:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.