Indentation

The technical definition of indentation is discussed in Reference Manual, 2.1.8.

Main points:

  1. Indentation is significant in Python. It is part of the language.
  2. Indentation consists leading whitespace (spaces and tabs) at the beginning of a [logical] line.
  3. Long lines may be ended by the \ character. The next line is then viewed as part of the preceding line (the two lines form a "logical line") and leading white space is no more significant than white space anywhere on a line.
  4. Good advice! 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. [and certainly on a single line!]
  5. It should also be noted that different platforms may explicitly limit the maximum indentation level.
  6. Indentation marks "blocks of code" and is ONLY legal to mark blocks of code. Code blocks include but are not limited to function definitions, if statements for loops, while loops, if try blocks
Indenting starts a block. Unindenting ends it.

Here is an example from the Python reference model of a correctly (though confusingly) indented piece of Python code:

def perm(l):
        # Compute the list of all permutations of l
    if len(l) <= 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
Note especially the comment line. This simply does not count as a line in the function and therefore does not set the indentation level for succeeding lines. This is in contrast to:
def perm(l):
        """Compute the list of all permutations of l"""
    if len(l) <= 1:                  # Error here!
                  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
This is an error.

A string at the beginning of a Python function definition is always interpreted as a documentation string and becomes an attribute of the function object. Since it is part of the language it must follow the usual rules of indentation. In particular, it sets the indentation level for succeeding lines, so the error is on the next line, which "unindents" to a column not previously used by any block.

Here is the same piece of code more consistently and sensibly indented. This is better than the indentation above but the difference is matter of style ( Python Styleguide, discusses indentation), not correctness.

def perm(l):
    # Compute the list of all permutations of l
    if len(l) <= 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

The following example shows various indentation errors. These will raise exceptions when loaded into Python.

 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