6 ms·
Question: why don't pythonistas use tabs instead of spaces for indentation? To my knowledge pretty much every editor can be customized to display tabs in whatev
by chaostheory 15y ago
Question: why don't pythonistas use tabs instead of spaces for indentation? To my knowledge pretty much every editor can be customized to display tabs in whatever length users want e.g. 2 spaces, 4 spaces, etc... imo it's a lot easier than dealing with different sets of code with a varying number of spaces for indentation.
- veyron 15y agotabs are interpreted as exactly 8 spaces, and oftentimes people set editors to use less than 8 spaces
- nhaehnle 15y agoWhy is that a problem though? If you consistently always use tabs for indentation, this interpretation does not matter.
- burgerbrain 15y agoWhy does it matter to you if people set their editors that way? What is your point here?
- veyron 15y agoSome people write assuming that 8 spaces and tabs are interchangeable. I can change my habits but cant change others. And in those cases, I have to set my tab width to 8 spaces to read it properly. I'm not condoning it -- but you have to realize that oftentimes we have to interact with others' code
- ominous_prime 15y agoThe standard indentation is 4 spaces, and people often have their tabwidth set to 4 spaces. Sometimes tabbed code (often older code, before spaces were really standard), end up with mixed tabs and spaces. The logic of the code then reads completely differently depending on your editor settings.
- burgerbrain 15y agoI'm not advocating people mix tabs and spaces, I'm advocating that people always use tabs so that others can display it as they wish.
- Goladus 15y agoI think most people would rather use spaces for their own sanity. Most people would prefer not to bother adjusting tabwidths, ever. So the capability to adjust the display of code to individual's liking would be largely unappreciated. In other words, forcing everyone to use tab-characters for spacing would be a burden on the majority for the benefit of a tiny minority.
- jamesgeck0 15y agoThe majority has to change their editor's settings to insert four spaces instead of a tabulation, anyway. Well, unless you're just reading code.
- Goladus 15y agoI suspect most python developers get the "insert four spaces per tab" for free with whatever IDE they use.
- ominous_prime 15y agoThere's no drawback if you're working alone. The rest of the community however, decided that spaces are the standard. Yes it's somewhat arbitrary (golang picked tabs for instance), but if you don't want to conform to the community coding standards, it only brings hardship upon yourself. A lot of python is convention based, and you can break all the rules you want and still have valid code, but no one else will want to use or maintain it.
- Peaker 15y agoIf you use tabs everywhere, your code alignment will be all wrong: if x: ----some_long_func_name(..., ---- blah... Note that ---- is indentation and the spaces need to be spaces. If you use tabs for all of this, it will come out misaligned. You could say -- then why not use tabs for the indent part, and spaces for the alignment part? Because virtually no editors support doing this automatically (and doing this manually is not practical). The few editors that do support automating this, don't make it discoverable (let alone a default). What you end up with is a horrible mixture of tabs and spaces, or if you use tabs only, then code that does not align right. Using tabs simply does not work in practice. Spaces may have the (supposed) disadvantage of a fixed indentation size, but they really are the only way to make indented code work across different editors. And this is not an optional thing, therefore tabs should be banned, completely.
- Dylan16807 15y agoBut the fact that python does this is a flaw. There would be no problems if it forced you to use the same spacing characters in a block instead of just the same spacing count. For example. if foo: [tab]bar #valid if foo: bar #valid if foo: if baz: bar #valid if foo: if baz: [tab]bar #invalid Your four spaces disappeared but it wasn't a DEDENT. if foo: if baz: [tab]bar #This can be valid, I guess. Doesn't harm anyone. Once you don't allow people to interchange tabs and spaces it's perfectly safe to set tab width to any amount.
- sesqu 15y agoExactly. I strongly dislike the way indentation is assumed to be done with x spaces. I quite like my indents of size 1 tab, and would hope the lexer differentiates between <tab><3 space><tab> and <tab><tab><3 space>, since I certainly do.
- veyron 15y agotabs are interpreted as exactly 8 spaces, and oftentimes people set editors to use less than 8 spaces
- prodigal_erik 15y agoThe article claims tabs are more likely to be shredded than spaces, but I didn't find that to be true. Half the tools on the Web are going to irreparably shred Python code no matter which you use. I still think Ruby is displacing it largely because it's actually possible to talk about Ruby code without ridiculous workarounds like pastebins.
- parfe 15y ago>The article claims tabs are more likely to be shredded than spaces, but I didn't find that to be true. def x():<enter> <tab>"Oh no I'm on the reply button!"<Shift+Tab><space><space><space><space>print "Much Better"
- prodigal_erik 15y agoThe problem isn't entering a tab character (I'd be pasting code from a real editor anyway). The problem is that whatever indentation you use, whether it's tabs or spaces, is destroyed by many common tools, and un-indented Python is ambiguous and can't be fixed without guesswork.
- Goladus 15y agoTabs are more likely to be destroyed than spaces. Simply displaying a file in a character-based terminal shreds tabs. Spaces are fine. It's true there are tools, especially on the web, that eliminate whitespace and if you rely on those tools using python is going to be a problem. But generally it's pretty obvious whether that's going to be an issue. Tab problems tend to sneak up when you least expect it.
- ominous_prime 15y agoAt some point, people need to decide on a standard, which was eventually documented as pep8. Since whitespace is significant within the language, two groups using different conventions may run into problems inter-operating. In older tabbed python code, I've seen numerous patches fail to run, because the patch had the wrong indentation scheme. Worse, were patches I've found that ran, but with subtle bugs, because the change in indentation modified the logic without runtime errors.
- masklinn 15y agoBecause spaces allow for indentation and alignment in every editor under the sun. Also, because tab is a pointless character and an outdated hack.
- ajanuary 15y agoWhich is an argument for mixing tabs and spaces. Which Python doesn't very well handle.
- pjkundert 15y agoBecause redefining tab width as something other than 8 is, quite frankly, a disaster. The problem is, a "tab" does not advance by 8 spaces; it advances to the next column divisible by 8! Shockingly, most editor writers don't seem to realize this; this appears to be especially true in the Windows world (perhaps because they never used typewriters or ASCII terminals?) The resultant code is only readable in an editor with the same settings; for a language like Python, the resultant code will, in fact, be semantically incorrect. Some editors that are not broken (eg. emacs, vim), allow you to specify a desired indentation level, and then the editor constructs the correct combination of tabs and spaces (ideally just spaces, for Python) to indent the code to the correct depth. This comes from someone who has to regularly recover code mangled by dozens of Windows "programmers" who edit code on so-called "Programmers" editors with alternate tab settings...
- prakashk 15y ago> why don't ... use tabs instead of spaces for indentation? http://www.jwz.org/doc/tabs-vs-spaces.html http://www.jwz.org/doc/tabs-vs-spaces.html