Indentation problems caused by using tabs, with the tab size in the editor configured to 4 spaces.
- indentation
- tabs
Python 2 and 3 allow tabs to be used to indent lines. Python will always expand these to a multiple of 8 (with Python 3 throwing an exception if there were spaces preceding the tab), but many editors and Stack Overflow expand tabs to multiples of 4 instead. This can lead to subtle and weird flow errors.
The canonical example is a return
statement that uses a tab, placed after a for
loop using spaces for indentation; it looks like the return
is indented to 4 spaces, but in reality it is part of the loop:
def test(word):
for l in word:
print l
return total
The function is exited during the first iteration, rather than print all elements of word
first.
Running with python -tt
won’t help in this class of errors.