DOS, UNIX and tabs

Duncan Booth duncan.booth at invalid.invalid
Thu Dec 28 07:35:27 EST 2006


Felix Benner <felix.benner at imail.de> wrote:

> So what's the point in typing four spaces for indentation instead of one
> tab?

So long as you always use only tabs there is no problem. So long as you 
only use spaces there is no problem. If you mix tabs and spaces you can 
introduce bugs. In particular, some people set their editor up to expand 1 
tab to the next multiple of 4 spaces on their screen, but the usual 
convention for tabs (and one that Python follows internally) is that tabs 
expand to the next multiple of 8 spaces.

Usually when you mix spaces and tabs what you get either works the way you 
intend, or it generates a syntax error. Once however when this recurring 
question popped up I did a search through a load of Python files and 
actually found once instance of some code which had been released and ran  
whether tabs were expanded to 4 or 8 space boundaries. Reading that code it 
was apparent that it had been written using 4 space tabs on the screen, but 
that when it ran it did something different than had been intended.

So, given that mixing tabs and spaces is deadly choose one or the other and 
stick to it. If you intend to work with other people then choose the same 
convention as they use. If you are never going to work with others then use 
whichever scheme makes you most comfortable.

Be careful as not all open source projects use the same convention: in 
previous discussions on this newsgroup there were people arguing quite 
strongly for using the tab convention. A straw poll indicated that there 
was 1 open source project with 3 developers using tabs, and all other open 
source projects use spaces only as the stated (but not always strictly 
enforced) convention. Your experience may of course differ.

Of course nobody in their right minds actually types 4 spaces for 
indentation: they use an editor where if the automatic indentation 
isn't correct then hitting the tab key inserts the correct number of spaces 
(and with luck where hitting the backspace key deletes back to the previous 
tabstop).



More information about the Python-list mailing list