Python's interpretation of tabs and spaces to indent

Question:

I decided, that I learn a bit of Python. The first introduction says that it uses indentation to group statements. While the best habit is clearly to use just one of these what happens if I interchange them? How many spaces will be considered equal to one tab? Or will it fail to work at all if tabs and spaces are mixed?

Asked By: Lukas

||

Answers:

Four spaces are one tab (in my setup), but as far as I know, they are not interchanged. You can use either spaces or tabs, not both.

Answered By: Amirshk

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 "fffn"
    print "yyyn"

The ‘^I‘ shows a TAB. When run through Python 2.5, I get the error:

  File "xx.py", line 4
    print "yyyn"
                ^
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 1n"
        print "y is unsetn"

In Python 2, this code works, printing nothing.


In Python 3, the rules are slightly different (as noted by Antti Haapala). Compare:

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’.)

Answered By: Jonathan Leffler

Just don’t interchange them 🙂
Set your IDE/editor to input 4 spaces upon pressing “tab” and you are good to go.

Answered By: shylent

I would recommend that you go through PEP 8 which is the ‘official’ Python style guide for Python code. It covers (among other things) the use of tabs/spaces.

Answered By: Noufal Ibrahim

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!

Answered By: Eli Bendersky

I believe that the tab character should simply never appear in source code under any circumstances. There’s no advantage to it and it’s an endless source of tiny errors. – use a character string with t if you need a tab, it has the advantage that it’s self-documenting.

Here‘s the classic article about tabs vs spaces – I use a variant of jwz’s elisp in my own .emacs file.

(I confess to personally breaking with PEP 8 by using only 2 characters’ indentation – 4 characters is a lot when your lines are only 80 characters…)

Answered By: Tom Swirly

In Python 2, the interpretation of TAB is as if it is converted to spaces using 8-space tab stops (as provided by previous answers already); that is that each TAB furthers the indentation by 1 to 8 spaces so that the resulting indentation is divisible by 8.

However this does not apply to Python 3 anymore – in Python 3 mixing of spaces and tabs are always an error – tabs only match tabs and spaces only match other spaces in indentation; that is a block indented with TABSPACESPACE might contain a block indented with TABSPACESPACETAB, but not one indented with TABTABTAB, it would be considered an indentation error, even though the block would seemingly extend further:

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.

I.e. the algorithm works as follows:

  • if both number of tabs and number of spaces matches the previous line (no matter the order), then this line belongs to the same block with the previous line

  • if the number of one of (tabs, spaces) is greater than on the previous line and number of the other is at least equal to those on the previous line, this is an indented block

  • the tuple (tabs, spaces) matches an indent from a previous block – this dedents to that block

  • otherwise an IndentationError or a TabError is raised.

This is why mixing tabs and spaces, or even using tabs for indentation at all would be considered a very bad practice in Python.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.