Spaces are not treated as equivalent to tab. A line indented with a tab is at a different indentation from a line indented with 1, 2, 4 or 8 spaces.
Proof by counter-example [erroneous, or, at best, limited - tab != 4 spaces]:
x = 1
if x == 1:
^Iprint "fff\n"
print "yyy\n"
The '^I
' shows a TAB. When run through Python 2.5, I get the error:
File "xx.py", line 4
print "yyy\n"
^
IndentationError: unindent does not match any outer indentation level
Thus showing that in Python 2.5, tabs are not equal to spaces [and in particular not equal to 4 spaces].
Oops - embarrassing; my proof by counter-example shows that tabs are not equivalent to 4 spaces. As Alex Martelli points out in a comment, in Python 2, tabs are equivalent to 8 spaces, and adapting the example with a tab and 8 spaces shows that this is indeed the case.
x = 1
if x != 1:
^Iprint "x is not 1\n"
print "y is unset\n"
In Python 2, this code works, printing nothing.
In Python 3, the rules are slightly different [as noted by Antti Haapala]. Compare:
- Python 2 on Indentation
- Python 3 on Indentation
Python 2 says:
First, tabs are replaced [from left to right] by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight [this is intended to be the same rule as used by Unix]. The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.
Python 3 says:
Tabs are replaced [from left to right] by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight [this is intended to be the same rule as used by Unix]. The total number of spaces preceding the first non-blank character then determines the line’s indentation. Indentation cannot be split over multiple physical lines using backslashes; the whitespace up to the first backslash determines the indentation.
[Apart from the opening word "First," these are identical.]
Python 3 adds an extra paragraph:
Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.
This means that the TAB vs 8-space example that worked in Python 2 would generate a TabError in Python 3. It is best — necessary in Python 3 — to ensure that the sequence of characters making up the indentation on each line in a block is identical. PEP8 says 'use 4 spaces per indentation level'. [Google's coding standards say 'use 2 spaces'.]
Hey Guys,
I've been following a Guide to python where they told us to use Tabs but then they sent us to the PEP8 Style Guide where it states:
"Spaces are the preferred indentation method."
Now, after I digging around a bit I found that you should use 4 spaces to indent code, sooo...
Why not just use Tab instead of hitting the space bar 4 times?
I'm guessing this is a hot topic, but I just wanted to make sure it's ok with going with Tabs over spaces [since it's the same lenght!]. [I'm also a fan of Silicon Valley and as Richard Hendriks says: "2 Tabs save you 8 spaces"!]
/ @treyhunnerIndentation
Some programmers just want to mix tabs and spaces
Indentation doesn't always matter
def flatten[matrix]:
return [
n for row
in matrix for n
in row
]
def len_or_none[obj]:
try:
return len[obj]
except TypeError:
return None
Same block? Indentation matters.
def guess_number[]:
while True:
guess = input['Guess: ']
if guess == '4':
break
def len_or_none[obj]:
try:
return len[obj]
except TyperError:
return None
Indentation rules for each line of code
- Same block: last line's indentation level
- Outer block: a previous less-indented level
- New block: more indented than current indentation
What about tabs and spaces?
- Typewriters: tab key moves the cursor to the next tab stop
- In Python, there's a tab stop at every 8 characters
- 1 tab character = number of spaces until the next tab stop
Tab stops are 8 characters
def guess_number[]:
while True:
guess = input['Guess: ']
if guess == '4':
break
def guess_number[]:
while True:
guess = input['Guess: ']
if guess == '4':
break
Tabs: not always 8 spaces
def guess_number[]:
while True:
guess = input['Guess: ']
if guess == '4':
break
def guess_number[]:
while True:
guess = input['Guess: ']
if guess == '4':
break
def progressively_more_spaces[]:
print["let's add one more space to the beginning..."]
print["of each line."]
print["The effective indentation is the same"]
print["even though we're adding more spaces."]
print["Each tab represents only the spaces needed"]
print["to get to the next 8 character tab stop."]
if "your editor doesn't use 8 character tabs":
print["you may have trouble reading this code."]
print["Python uses 8 character tab stops"]
print["so we also use 8 character tab stops."]
print["This way we can mix spaces and tabs"]
print["without any worries about broken code."]
print["Mixing tabs and spaces can be fun"]
print["but beware!"]
while "you could write code like this":
print["It's probably best not to."]
Configure your editor
# //editorconfig.org
[*.py]
indent_size = 4
tab_width = 8
indent_style = tab
Don't use Python 3
$ python3 examples/demo.py
File "examples/demo.py", line 3
print["of each line."]
^
TabError: inconsistent use of tabs and spaces in indentation
PEP 373
End Of Life for Python 2.7: 2020
Don't mix tabs and spaces
Trey Hunner / @treyhunner
Python trainer, on-site & remote
//truthful.technology