user537488
user537488

Reputation: 937

Python Tabs Fake or Real

Many tutorial suggest users to use tab with width of 4 while 4 is made using spaces not real tab character. Using real tab also it works so why do people suggest to use spaces of 4 rather than real tab ? I'm a python beginner confused ????

[UPDATE] Thanks! I am now switching to spaces of 4.

Added following to my .vimrc "Python

au BufRead,BufNewFile *.py,*pyw set shiftwidth=4
au BufRead,BufNewFile *.py,*.pyw set expandtab
fu Select_c_style()
    if search('^\t', 'n', 150)
        set shiftwidth=8
        set noexpandtab
    el 
        set shiftwidth=4
        set expandtab
    en
endf

For other case:

set noexpandtab                " tabs are tabs, not spaces

Upvotes: 0

Views: 860

Answers (4)

user483040
user483040

Reputation:

Many, if not most editors will do an inline conversion of tabs to a user defined number of spaces. Set that to 4 and you are home free. The real pain comes when you are on a team that hasn't standardized on this idea of 4 spaces.

Upvotes: 1

khachik
khachik

Reputation: 28703

The only reason I can consider may be that if you don't have set up your editor to use tabstop=4 (say, in VIM) your code lines will be longer and it will be inconvenient to read such a code. The only rule should be not to mix tabs and spaces, I think (I have seen code where the single indent was four spaces and the double indent was a tab. It was horrible.)

Upvotes: 1

Adam Vandenberg
Adam Vandenberg

Reputation: 20671

As soon as you allow a tab character into a file, you will end up in a situation with mixed tabs and spaces, and then you will start to get parsing errors. This gets worse when multiple people are working on a project, and have their spacing settings set different from each other.

PEP8 recommends "4 spaces": http://www.python.org/dev/peps/pep-0008/

And this recommendation is made with experience, dealing with the kinds of problems that mixing tabs and spaces in Python brings. Much, much better to save yourself the pain, and just use 4 spaces for your Python code.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799160

The Python compiler considers the tab character to be equivalent to indentation of 8 spaces. You can use tabs if you like but mixing tabs and spaces will lead to tricky-to-debug errors, which is why sticking with spaces is recommended.

Upvotes: 2

Related Questions