views:

326

answers:

6

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?

A: 

4 spaces are one tab ( in my setup ) but as far as i know, they are not intercahnged. you can use either spaces or tabs. not both.

Am
Not necessarily. That depends on your setup.
Noufal Ibrahim
+6  A: 

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, 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"

This code works, printing nothing.

Changed to Community Wiki since I don't deserve any (extra) credit.

Jonathan Leffler
Unfortunately, tabs ARE considered equivalent to spaces -- specifically, eight spaces, as the docs say quite clearly. Never mixing tabs and spaces is best-practice, but you need to run Python with `-t` (or `-tt`) to get warnings (or errors) for violations of said best practice.
Alex Martelli
+2  A: 

Just don't interchange them :)
Set your IDE/editor to input 4 spaces upon pressing "tab" and you are good to go.

shylent
+3  A: 

I would recommend that you go through http://www.python.org/dev/peps/pep-0008/ which is the 'official' python style guide for Python code. It covers (among other things) the use of tabs/spaces.

Noufal Ibrahim
+7  A: 

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!
Eli Bendersky
A: 

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

Tom Swirly