Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's interpretation of tabs and spaces to indent

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?

like image 527
Lukas Avatar asked Jan 09 '10 18:01

Lukas


People also ask

Can you indent with tabs in Python?

Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

How many spaces does tab indent in Python?

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.

Why does Python use spaces indented?

Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right.

How do you fix TabError inconsistent tabs and spaces in indentation in Python?

The Python "TabError: inconsistent use of tabs and spaces in indentation" occurs when we mix tabs and spaces in the same code block. To solve the error, remove the spacing and only use tabs or spaces, but don't mix the two in the same code block.


2 Answers

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

like image 99
5 revs, 2 users 97% Avatar answered Sep 22 '22 13:09

5 revs, 2 users 97%


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!

like image 40
Eli Bendersky Avatar answered Sep 19 '22 13:09

Eli Bendersky