You are not logged in.

#1 2012-12-08 17:17:34

DarkSotM
Member
From: Washington
Registered: 2011-10-02
Posts: 7

python pep 8 spaces over tabs?

So I've slowly been teaching myself python and was reading PEP 8, the style guideline.  It all makes sense I was just hoping for a little insight.  In it, it says

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!

For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

I understand not mixing them.  And I don't want to start a flame war.  But why would hitting the space bar 4 times be preferred over the tab key once?  I guess I ask because I've just instinctively hit the tab key.

Offline

#2 2012-12-08 17:36:52

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: python pep 8 spaces over tabs?

Because a sane text editor will let you hit tab and insert N spaces for you instead of a tab character.

# vim: set et ts=4 sw=4:

Offline

#3 2012-12-08 17:38:41

ijanos
Member
From: Budapest, Hungary
Registered: 2008-03-30
Posts: 443

Re: python pep 8 spaces over tabs?

Don't get confused, no one hits the space key 4 times. The thing is every decent text editor and IDE can be configured to insert 4 spaces when you hit the tab key. Ideally they will even delete the 4  spaces for you if you press the backspace key.

Some editors defaults to spaces when you open a .py file, so it is possible that your source files already contains spaces even if you used the tab key.

Offline

#4 2012-12-09 11:08:15

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: python pep 8 spaces over tabs?

Hitting the tab key is overrated, proper editors like vim will indent correctly for you. I almost never have to indent manually when coding in python (or most other languages).


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#5 2012-12-09 19:49:18

Nisstyre56
Member
From: Canada
Registered: 2010-03-25
Posts: 85

Re: python pep 8 spaces over tabs?

Some people prefer having the tab key enter n spaces as falconindy said. Whether you want an actual '\t' character or just 4 spaces depends on whether you want your code to show up the same in all text editors, rather than what the tab character looks like. I don't really think there are any good arguments for using \t over spaces myself, however this seems to be a touchy subject, so don't listen to me on that.

Also, yes, with a proper customized lexer for whatever language you're using, your editor should indent automagically.

Last edited by Nisstyre56 (2012-12-09 19:50:12)


In Zen they say: If something is boring after two minutes, try it for four. If still boring, try it for eight, sixteen, thirty-two, and so on. Eventually one discovers that it's not boring at all but very interesting.
~ John Cage

Offline

#6 2012-12-12 17:03:24

Kaonashi
Member
Registered: 2012-12-11
Posts: 26

Re: python pep 8 spaces over tabs?

I'm just used to using \t characters, too lazy to configure my text editors to insert 4 spaces

Offline

#7 2012-12-12 17:53:34

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: python pep 8 spaces over tabs?

The only real argument is that all of those spaces are wasting bytes. It doesn't matter on most systems, but just think of how much diskspace is wasted throughout the world to hold those extra characters.

I actually prefer spaces for some reason, but I only use 2 per indentation level. It also helps if you try to keep your code within the first 80 columns of the file.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#8 2012-12-12 18:39:41

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: python pep 8 spaces over tabs?

Xyne wrote:

I actually prefer spaces for some reason, but I only use 2 per indentation level. It also helps if you try to keep your code within the first 80 columns of the file.

I think the reason you (and I) prefer spaces is because of the "logical" feel of it: Every character takes the same amount of space. If I want to go to a specific location in a file, I simply click on it. But tabs break this consistency. Code no longer looks exactly the same in every text editor, and you can't position your cursor anywhere in the file (only at the end of a tab).

Anyway, it's just a thought I had.

The only real argument is that all of those spaces are wasting bytes. It doesn't matter on most systems, but just think of how much diskspace is wasted throughout the world to hold those extra characters.

Can't tell if serious... tongue

Offline

#9 2012-12-12 19:16:01

Pajaro
Member
Registered: 2004-04-21
Posts: 884

Re: python pep 8 spaces over tabs?

tabs allow you to display indentation to your liking.
usually, as more experienced you are less indentation you want.
spaces are fixed and suck because of that.

Offline

#10 2012-12-12 19:29:43

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: python pep 8 spaces over tabs?

drcouzelis wrote:

the "logical" feel of it

When coding, I think of each level of indentation as a discrete 'characteristic'.  Being able to put my cursor "in the middle of an indent" is not something I find logical.  If it weren't for the super-powers of vim, I'd use tabs for all my indentation needs - guidelines be damned.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#11 2012-12-15 15:02:24

essence-of-foo
Member
Registered: 2008-07-12
Posts: 84

Re: python pep 8 spaces over tabs?

Technically, the best way is to use tabs for indention and spaces for alignment: http://emacswiki.org/emacs/SmartTabs

However, in practise this causes problems because some people just don't understand it.

Using only spaces are a no-brainer because you can't do anything wrong with them.

For my personal programs I use tabs for indention and spaces for alignment. For other projects I stick to the convention that's already there.

And I like it when my tabs are 8 characters wide. Some people prefer other settings. By distinguishing between indention and alignment, everyone can be happy.

Last edited by essence-of-foo (2012-12-15 15:04:02)

Offline

Board footer

Powered by FluxBB