Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82011 > unrolled thread

Tabs for indentation & Spaces for alignment in Python 3?

Started byAahan Krish <krish@aahan.me>
First post2014-12-05 17:40 +0000
Last post2014-12-05 10:50 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Tabs for indentation & Spaces for alignment in Python 3? Aahan Krish <krish@aahan.me> - 2014-12-05 17:40 +0000
    Re: Tabs for indentation & Spaces for alignment in Python 3? sohcahtoa82@gmail.com - 2014-12-05 10:50 -0800

#82011 — Tabs for indentation & Spaces for alignment in Python 3?

FromAahan Krish <krish@aahan.me>
Date2014-12-05 17:40 +0000
SubjectTabs for indentation & Spaces for alignment in Python 3?
Message-ID<mailman.16632.1417801617.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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?

*Q2.* PEP 8 also reads, "Python 3 disallows mixing the use of tabs and
spaces for indentation."

So, if I am using tabs for indentation and spaces for alignment, is it
still considered "mixing the use of tabs and spaces" in Python 3? (Please
see this example
<http://lea.verou.me/2012/01/why-tabs-are-clearly-superior/#css>.)

Thank you very much for your time!


Best
Aahan

[toc] | [next] | [standalone]


#82017

Fromsohcahtoa82@gmail.com
Date2014-12-05 10:50 -0800
Message-ID<688383a6-2353-4fc3-a3ab-bc7c1857973c@googlegroups.com>
In reply to#82011
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web