Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="whitespace.html" /> | |
13 | <link rel="prev" href="blank-lines.html" /> | |
14 | <link rel="parent" href="line-structure.html" /> | |
15 | <link rel="next" href="whitespace.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>2.1.8 Indentation</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="2.1.7 Blank lines" | |
25 | href="blank-lines.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="2.1 Line structure" | |
28 | href="line-structure.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="2.1.9 Whitespace between tokens" | |
31 | href="whitespace.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="blank-lines.html">2.1.7 Blank lines</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="line-structure.html">2.1 Line structure</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="whitespace.html">2.1.9 Whitespace between tokens</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION004180000000000000000"></A><A NAME="indentation"></A><a id='l2h-9' xml:id='l2h-9'></a> | |
56 | <BR> | |
57 | 2.1.8 Indentation | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | Leading whitespace (spaces and tabs) at the beginning of a logical | |
62 | line is used to compute the indentation level of the line, which in | |
63 | turn is used to determine the grouping of statements. | |
64 | ||
65 | <P> | |
66 | First, tabs are replaced (from left to right) by one to eight spaces | |
67 | such that the total number of characters up to and including the | |
68 | replacement is a multiple of | |
69 | eight (this is intended to be the same rule as used by <span class="Unix">Unix</span>). The | |
70 | total number of spaces preceding the first non-blank character then | |
71 | determines the line's indentation. Indentation cannot be split over | |
72 | multiple physical lines using backslashes; the whitespace up to the | |
73 | first backslash determines the indentation. | |
74 | ||
75 | <P> | |
76 | <strong>Cross-platform compatibility note:</strong> because of the nature of | |
77 | text editors on non-UNIX platforms, it is unwise to use a mixture of | |
78 | spaces and tabs for the indentation in a single source file. It | |
79 | should also be noted that different platforms may explicitly limit the | |
80 | maximum indentation level. | |
81 | ||
82 | <P> | |
83 | A formfeed character may be present at the start of the line; it will | |
84 | be ignored for the indentation calculations above. Formfeed | |
85 | characters occurring elsewhere in the leading whitespace have an | |
86 | undefined effect (for instance, they may reset the space count to | |
87 | zero). | |
88 | ||
89 | <P> | |
90 | The indentation levels of consecutive lines are used to generate | |
91 | INDENT and DEDENT tokens, using a stack, as follows. | |
92 | ||
93 | <P> | |
94 | Before the first line of the file is read, a single zero is pushed on | |
95 | the stack; this will never be popped off again. The numbers pushed on | |
96 | the stack will always be strictly increasing from bottom to top. At | |
97 | the beginning of each logical line, the line's indentation level is | |
98 | compared to the top of the stack. If it is equal, nothing happens. | |
99 | If it is larger, it is pushed on the stack, and one INDENT token is | |
100 | generated. If it is smaller, it <em>must</em> be one of the numbers | |
101 | occurring on the stack; all numbers on the stack that are larger are | |
102 | popped off, and for each number popped off a DEDENT token is | |
103 | generated. At the end of the file, a DEDENT token is generated for | |
104 | each number remaining on the stack that is larger than zero. | |
105 | ||
106 | <P> | |
107 | Here is an example of a correctly (though confusingly) indented piece | |
108 | of Python code: | |
109 | ||
110 | <P> | |
111 | <div class="verbatim"><pre> | |
112 | def perm(l): | |
113 | # Compute the list of all permutations of l | |
114 | if len(l) <= 1: | |
115 | return [l] | |
116 | r = [] | |
117 | for i in range(len(l)): | |
118 | s = l[:i] + l[i+1:] | |
119 | p = perm(s) | |
120 | for x in p: | |
121 | r.append(l[i:i+1] + x) | |
122 | return r | |
123 | </pre></div> | |
124 | ||
125 | <P> | |
126 | The following example shows various indentation errors: | |
127 | ||
128 | <P> | |
129 | <div class="verbatim"><pre> | |
130 | def perm(l): # error: first line indented | |
131 | for i in range(len(l)): # error: not indented | |
132 | s = l[:i] + l[i+1:] | |
133 | p = perm(l[:i] + l[i+1:]) # error: unexpected indent | |
134 | for x in p: | |
135 | r.append(l[i:i+1] + x) | |
136 | return r # error: inconsistent dedent | |
137 | </pre></div> | |
138 | ||
139 | <P> | |
140 | (Actually, the first three errors are detected by the parser; only the | |
141 | last error is found by the lexical analyzer -- the indentation of | |
142 | <code>return r</code> does not match a level popped off the stack.) | |
143 | ||
144 | <P> | |
145 | ||
146 | <DIV CLASS="navigation"> | |
147 | <div class='online-navigation'> | |
148 | <p></p><hr /> | |
149 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
150 | <tr> | |
151 | <td class='online-navigation'><a rel="prev" title="2.1.7 Blank lines" | |
152 | href="blank-lines.html"><img src='../icons/previous.png' | |
153 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
154 | <td class='online-navigation'><a rel="parent" title="2.1 Line structure" | |
155 | href="line-structure.html"><img src='../icons/up.png' | |
156 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
157 | <td class='online-navigation'><a rel="next" title="2.1.9 Whitespace between tokens" | |
158 | href="whitespace.html"><img src='../icons/next.png' | |
159 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
160 | <td align="center" width="100%">Python Reference Manual</td> | |
161 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
162 | href="contents.html"><img src='../icons/contents.png' | |
163 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
164 | <td class='online-navigation'><img src='../icons/blank.png' | |
165 | border='0' height='32' alt='' width='32' /></td> | |
166 | <td class='online-navigation'><a rel="index" title="Index" | |
167 | href="genindex.html"><img src='../icons/index.png' | |
168 | border='0' height='32' alt='Index' width='32' /></A></td> | |
169 | </tr></table> | |
170 | <div class='online-navigation'> | |
171 | <b class="navlabel">Previous:</b> | |
172 | <a class="sectref" rel="prev" href="blank-lines.html">2.1.7 Blank lines</A> | |
173 | <b class="navlabel">Up:</b> | |
174 | <a class="sectref" rel="parent" href="line-structure.html">2.1 Line structure</A> | |
175 | <b class="navlabel">Next:</b> | |
176 | <a class="sectref" rel="next" href="whitespace.html">2.1.9 Whitespace between tokens</A> | |
177 | </div> | |
178 | </div> | |
179 | <hr /> | |
180 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
181 | </DIV> | |
182 | <!--End of Navigation Panel--> | |
183 | <ADDRESS> | |
184 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
185 | </ADDRESS> | |
186 | </BODY> | |
187 | </HTML> |