Python: Myths about Indentation

“Whitespace is significant in Python source code.”

No, not in general. Only the indentation level of your
statements is significant (i.e. the whitespace at the
very left of your statements). Everywhere else, whitespace
is not significant and can be used as you like, just like
in any other language. You can also insert empty lines
that contain nothing (or only arbitrary whitespace)
anywhere.

Also, the exact amount of indentation doesn’t matter at all,
but only the relative indentation of nested blocks (relative
to each other).

Furthermore, the indentation level is ignored when you use
explicit or implicit continuation lines. For example,
you can split a list across multiple lines, and the indentation
is completely insignificant. So, if you want, you can do
things like this:

>>> foo = [
...            'some string',
...         'another string',
...           'short string'
... ]
>>> print foo

['some string', 'another string', 'short string']


>>> bar = 'this is ' 
...       'one long string ' 
...           'that is split ' 
...     'across multiple lines'
>>> print bar

this is one long string that is split across multiple lines

Leave a comment