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"!)
A tab is an actual character “\t”. It’s usually represented as a space of length 4, but is still a single distinct character. 4 spaces is by far the preferred standard, but no one actually types 4 spaces each time, all but the most extremely basic text editors have a setting to convert tabs to spaces. This is what I and most people would suggest.
The style guide says:
Spaces are the preferred indentation method
because there can be a problem if you mix tab and space characters. Different editors/IDEs can interpret a mixture of tabs and spaces in different ways, leading to different indentations. I personally have seen a recent case where a user was getting an indentation error from python but the editor he was using showed good code. Using another editor or simply listing the code on the screen showed the bad indentation.
The cause of this difference in interpretation probably goes back to when computers took over from typewriters, but the result is we have different bits of software interpreting mixed tabs and spaces in different ways. The simplest way out of the problem is to not mix tabs and spaces. So use an editor that inserts the correct number of spaces when you press the TAB key, deletes the expected number of spaces when you press the DEL key, and NEVER writes a TAB character ('\t') to your source file on disk.
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'.)
Follow PEP 8 for Python style. PEP 8 says: Indentation
Use 4 spaces per indentation level.
For really old code that you don't want to mess up, you can continue to use 8-space tabs.
Tabs or Spaces?
Never mix tabs and spaces.
The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
For code indentation, is a single tab equivalent to 2 spaces or 4 spaces?
Python: using 4 spaces for indentation. Why? - Stack Overflow
The Python tab setting should default to 4 spaces to respect PEP8 and the Google style guide
Why does Python see a tab as 8 spaces? - Stack Overflow
Everyone else uses 4 spaces. That is the only reason to use 4 spaces that I've come across and accepted. In my heart, I still want to use tabs (1 indent character per indent, makes sense, no? Separate indent from other whitespace. I don't care that tabs can be displayed as different widths, that makes no syntactic difference. The worst that can happen is that some of the comments don't line up. The horror!) but I've accepted that since the python community as a whole uses 4 spaces, I use 4 spaces. This way, I can assemble code from snippets others have written, and it all works.
I like the fact that four space characters nicely indents the inner code of a function, because def + one space makes four characters.
def·foo():
····pass