<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>2.1.7 Indentation</TITLE>
<META NAME="description" CONTENT="2.1.7 Indentation">
<META NAME="keywords" CONTENT="ref">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ref.css">
<LINK REL="next" HREF="whitespace.html">
<LINK REL="previous" HREF="blank-lines.html">
<LINK REL="up" HREF="line-structure.html">
<LINK REL="next" HREF="whitespace.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="whitespace.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="line-structure.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="blank-lines.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="whitespace.html">2.1.8 Whitespace between tokens</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="line-structure.html">2.1 Line structure</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="blank-lines.html">2.1.6 Blank lines</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION004170000000000000000"></A>
<BR>
2.1.7 Indentation
</H2>

<P>
Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
turn is used to determine the grouping of statements.
<a name="l2h-22"></a>
<a name="l2h-23"></a>
<a name="l2h-24"></a>
<a name="l2h-25"></a>
<a name="l2h-26"></a>
<a name="l2h-27"></a>
<a name="l2h-28"></a>

<P>
First, tabs are replaced (from left to right) by one to eight spaces
such that the total number of characters up to and including the
replacement is a multiple of
eight (this is intended to be the same rule as used by Unix).  The
total number of spaces preceding the first non-blank character then
determines the line's indentation.  Indentation cannot be split over
multiple physical lines using backslashes; the whitespace up to the
first backslash determines the indentation.

<P>
<b>Cross-platform compatibility note:</b> because of the nature of
text editors on non-UNIX platforms, it is unwise to use a mixture of
spaces and tabs for the indentation in a single source file.

<P>
A formfeed character may be present at the start of the line; it will
be ignored for the indentation calculations above.  A formfeed
characters occurring elsewhere in the leading whitespace have an
undefined effect (for instance, they may reset the space count to
zero).

<P>
The indentation levels of consecutive lines are used to generate
INDENT and DEDENT tokens, using a stack, as follows.
<a name="l2h-29"></a>
<a name="l2h-30"></a>

<P>
Before the first line of the file is read, a single zero is pushed on
the stack; this will never be popped off again.  The numbers pushed on
the stack will always be strictly increasing from bottom to top.  At
the beginning of each logical line, the line's indentation level is
compared to the top of the stack.  If it is equal, nothing happens.
If it is larger, it is pushed on the stack, and one INDENT token is
generated.  If it is smaller, it <i>must</i> be one of the numbers
occurring on the stack; all numbers on the stack that are larger are
popped off, and for each number popped off a DEDENT token is
generated.  At the end of the file, a DEDENT token is generated for
each number remaining on the stack that is larger than zero.

<P>
Here is an example of a correctly (though confusingly) indented piece
of Python code:

<P>
<dl><dd><pre class=verbatim>
def perm(l):
        # Compute the list of all permutations of l
    if len(l) &lt;= 1:
                  return [l]
    r = []
    for i in range(len(l)):
             s = l[:i] + l[i+1:]
             p = perm(s)
             for x in p:
              r.append(l[i:i+1] + x)
    return r
</pre></dl>

<P>
The following example shows various indentation errors:

<P>
<dl><dd><pre class=verbatim>
     def perm(l):                       # error: first line indented
    for i in range(len(l)):             # error: not indented
        s = l[:i] + l[i+1:]
            p = perm(l[:i] + l[i+1:])   # error: unexpected indent
            for x in p:
                    r.append(l[i:i+1] + x)
                return r                # error: inconsistent dedent
</pre></dl>

<P>
(Actually, the first three errors are detected by the parser; only the
last error is found by the lexical analyzer -- the indentation of
<tt>return r</tt> does not match a level popped off the stack.)

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="whitespace.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="line-structure.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="blank-lines.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="whitespace.html">2.1.8 Whitespace between tokens</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="line-structure.html">2.1 Line structure</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="blank-lines.html">2.1.6 Blank lines</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
