4

Using a tab width of 4 spaces has emerged as the primary choice in programming. So why doesn't Python see a tab as 4 spaces instead of 8?

Is there a (technical, historic, other) reason for this design decision? It feels like that could have saved developers from struggling with inconsistent indentation a lot.

danijar
  • 32,406
  • 45
  • 166
  • 297
  • This is why you should never mix tabs and spaces. You shouldn't need to know that. – SLaks May 01 '14 at 19:56
  • The old "tab = 8 spaces" comes from the world of mechanical typewriters. They were literally "hard wired" to stop at every 8th position when you pressed the TAB key. Then came variable tab stops (more modern machines - from about the 1950's on...). But the old value stuck in some places apparently. see http://superuser.com/a/355868/196531 – Floris May 01 '14 at 19:57
  • 1
    @SLaks Yes, we shouldn't mix both because of the Python standard treating tabs as an uncommonly large amount of spaces. – danijar May 01 '14 at 19:57
  • PEP8 suggesting that you use spaces rather than tabs should save developers from struggling with inconsistent indentation ;-). If you're stuck writing code where there's a question about tabs/spaces, I'd _always_ run with `python -t`, maybe even `python -tt` – mgilson May 01 '14 at 19:57
  • @mgilson I think PEP8 suggests the opposite, which is not using tabs at all. I kind of disagree with this decision. But of course in a team, all have to stick to conventions. – danijar May 01 '14 at 19:59
  • @danijar -- Yes, that's what I meant. My comment didn't make much sense when I said that PEP8 preferred tabs. Sorry about that. – mgilson May 01 '14 at 20:00
  • See discussion on this subject at http://stackoverflow.com/q/120926/1967396 – Floris May 01 '14 at 20:03
  • 1
    @Floris Haha, I actually came from this question. :-) Thought the difference between how most programmers see tabs and how Python interprets them is the underlying problem. – danijar May 01 '14 at 20:14

1 Answers1

4

Because the default tab size in Linux console is 8 spaces, and therefore most CLI text editors in Linux also default to 8 spaces. Most are also configurable, but it's been the default for ages.

Some older (C) code uses mixed indention to fake a 4 space tab - 1 indent == 4 spaces, 2 indents == 1 tab, 3 indents == 1 tab + 4 spaces, etc..... it was awful. Not sure if it was done intentionally in order to make the code easier to read, or if some editor did this automatically to simulate 4 space tabs. All I know is that I was using pico and it was a PITA working with those, especially when you needed to indent/dedent a whole block. :)

kitti
  • 14,663
  • 31
  • 49