0

I've read the flame wars over the use of spaces and tabs. When working with any markup language (when scope isn't very important and when pressing space 4 times is a PITA), I tend to minimize the tab width to just a space or two but I fluidly change to four full tabs for my other programming work.

It is a matter of convenience most of the time and it only translates into real errors when sharing or mixing code. So why they don't just declare a format for optional overrides such as #tab = 2 at the beginning of a file? There would be no scoping issues and it could be handled with 2 lines of code.

Update

Whoa, I didn't explain myself very clearly: I only use editors that handle the tab/space conversion automatically. The only cogent point I can understand in the criticisms I have read regarding Pythons use of white space is how borked the space/tab convention becomes when sharing code.

Perhaps my confusion about using "4 spaces" is because of my prior life in the publishing industry and our use of tabs not as a unit of white space but an abstract element used to describe a document's structure, not the display. Substituting it with 4 spaces has more to do with Unix's shitty 8-space convention than it does with elegance. If Python standardized on using tabs by default we would eliminate the layer of abstraction (literal visual spaces -> abstract tab) that is getting lost when we leave IDE land and go to Usenet/chat/email land. I thought that #tab=2 would be a nice way to ensure that any differences originating due to convenience formatting wouldn't muck things up elsewhere.

  • 5
    Probably because Python Has Opinions. There is only one way to do it and frankly it's just simpler if everyone agrees on one formatting style. I'd be annoyed if I had to reconfigure my editor just to jump between files. – jozefg Oct 4 '13 at 20:31
  • 2
    "pressing space 4 times is a PITA" -- every editor under the sun (except nano) allows you to press tab once instead. – user7043 Oct 4 '13 at 20:56
  • Set up your editor / IDE so it accepts your tabs as input but immediately converts them to the right number of spaces. – David Hammen Oct 4 '13 at 21:01
  • So you are asking, if C, C++, Java, C#... etc developers have flame wars over tabs/spaces, why don't python make their developers do the same? I think you answered your own question. I personally like the absence of these fights and the ability to view code in any diff/code review tool or any editor and have it be consistent and aligned properly. – DXM Oct 4 '13 at 22:00
  • 1
    In vim I do the following when editing python in my .vimrc file: :set expandtab to make pressing the tab key create spaces and :set tabstop=4 to make each time I press the tab key make 4 spaces. For emacs: emacswiki.org/emacs/NoTabs. Other editors will let you do the same, you just need to find out how, but in any case if you are using any sort of a good IDE there's no reason that you need to be pressing the space 4 times in a row. – shuttle87 Oct 5 '13 at 3:37
4

Python has established a format: Use spaces.

Quoting from the Python Styleguide (PEP 8):

Spaces are the preferred indentation method.

Tabs should be used solely to remain consistent with code that is already indented with tabs.

Python 3 disallows mixing the use of tabs and spaces for indentation.

Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.

Any decent text editor lets you configure how tabs should be handled, including converting all tabs to a fixed number of spaces. I have Sublime Text configured to 4 spaces, the recommended PEP 8 indentation size:

Use 4 spaces per indentation level.

2

Most of the problems that you are describing deal with text editors, not the language itself.

To start with, who do you know that is hitting the space bar 4 times? Every editor I have is setup to insert 4 spaces when you hit the tab button. That is very standard procedure. Unless you are using Notepad, then the problem is that you need to reconfigure your dev environment.

Second, why are you "sharing and mixing code" in a way that implies that everything is happening within one file? And how would a macro at the beginning of the file change anything?

At the end of the day, standard practice is to use 4 spaces as a tab in Python. Use it, get used to it, and heavily chastise anyone who deviates from it.

  • Yes, I too use an IDE and advanced text editors that abstract this away. I was thinking of c/p from examples found online or passed via email. – Indolering Oct 5 '13 at 3:32
  • +1 "Use it, get used to it, and heavily chastise anyone who deviates from it." – user69037 Oct 5 '13 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.