More fun with Python's indentation rules
December 3, 2006
Python's indentation rules apply only to logical lines (as the documentation is careful to spell out), although the indentation level only comes from the first physical line. This opens up various sorts of peculiarity and evil, some of which I mentioned last time.
There are two ways for multiple physical lines to be joined
into one logical lines: being explicitly continued with
Of the two, implicit line joining is more wacky. Explicit line
joining cannot have comments (except on the last physical
line; this is sort of spelled out in the comments rules) or completely blank lines
(although it can have lines that are blank except for the
(Note that blank physical lines are not necessarily the same thing as blank logical lines.)
Implicit line continuation takes priority over explicit line continuation, so you can write:
def foo(a, \ # testing b): pass
All of these rules are implemented in the
It's somewhat unfortunate that Python doesn't have any rules at all for the indentation levels of physical lines. I think it would not be difficult to have a basic requirement that (for example) continued lines had to be indented at least as much as the logical line that they're on.
(Triple-quoted strings are handled by a different mechanism that wouldn't be affected by this. Although it wouldn't necessarily be a bad thing if they were; the need to de-indent triple quoted strings is arguably one of those small Python warts. (Unfortunately it is a problem with no good answer, since it is a conflict between literal representations and program layout aesthetics.))
Written on 03 December 2006.
* * *