Tabs for indentation & Spaces for alignment in Python 3?

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Fri Dec 5 13:50:31 EST 2014


On Friday, December 5, 2014 9:47:10 AM UTC-8, Aahan Krish wrote:
> I have two general questions regarding Python that I couldn't find any good answers for. This is not the often-asked Tabs vs Spaces question, so kindly read it in whole.
> 
> Q1. This is not to debate the decision, but I really wanted to know the reason why PEP 8 chose to go with spaces instead of tabs. I read that tabs were initially preferred over spaces (esp. Mr. Rossum)? What caused the decision that spaces are better?
> 
> According to Core Python Programming (Book):
> 
> ...because tabs vary in the number of spaces depending on your system, we recommend not using tabs if there is any hint of cross-platform development.
> 
> Isn't that a feature of tabs? (i.e. allowing the user to configure the size of a tab stop in his editor, which has no affect whatsoever on the tab character itself or how it displays in another user's editor.)
> 
> Is it the difficulty to adhere to the Maximum Line Length (79) when using tabs? or is it simply because most of the Python programmers were using spaces (or were adamant on using them) at the time of the decision-making process?
> 

The problem comes when people use tabs for aligning code when a single statement spans multiple lines.  For example let's say your tabs are 4 spaces, and you have this block of code:

if someCondition:
    someVariable = someFunction(parameter1, parameter2, parameter3,
                                parameter4, parameter5)

If you align the third line (the one with parameter4 and parameter5) with more than 1 tab, then someone with their tab size set to something other than 4 spaces won't see the aligned code.

Of course, the correct solution *IF YOU ARE USING TABS FOR INDENTATION* is to indent that third line with a SINGLE tab, then use a bunch of spaces to align the code.  This way, even if someone else has their tab size set to 8 spaces, they still see aligned code.

So using spaces instead of tabs is a way of preventing the code aligning issue.  The downside, of course, is that it forces everyone to use the same number of spaces for indentation, which results in a different argument of how many spaces should be used, which of course is a different discussion entirely, another dead horse to beat.

It of course is worth mentioning at this point that any editor that is worth using can be configured to insert spaces when you press the TAB key, and the number of spaces being configurable.  So even if you're using spaces, you can still use the TAB key.  Even the 'nano' editor for *nix can do this.  I frequently see a lot of beginner programmers using the argument that hitting the space bar 4-8 times (Depending on agreed tab size) is wasteful as an argument for tabs.



More information about the Python-list mailing list