WHY is there a TabError in Python? - Stack Overflow

I'm not asking WHAT is TabError nor HOW to fix TabError, I'm asking WHY.I completely underst

I'm not asking WHAT is TabError nor HOW to fix TabError, I'm asking WHY.

I completely understand a TabError means inconsistent use of tabs and spaces in indentation exactly as it prints. I mean, the Python Interpreter should be able to just simply convert each Tab to 4 spaces.

Is there any ambiguous situation that makes TabError sense?

The only one I can see is that, some may use 2 or other number of spaces as an indentation. But since Python accepts it, it knows it and is able to convert Tab to it.

I'm not asking WHAT is TabError nor HOW to fix TabError, I'm asking WHY.

I completely understand a TabError means inconsistent use of tabs and spaces in indentation exactly as it prints. I mean, the Python Interpreter should be able to just simply convert each Tab to 4 spaces.

Is there any ambiguous situation that makes TabError sense?

The only one I can see is that, some may use 2 or other number of spaces as an indentation. But since Python accepts it, it knows it and is able to convert Tab to it.

Share Improve this question asked Mar 8 at 13:24 KumaTeaKumaTea 6789 silver badges18 bronze badges 2
  • 2 Because that is the way the designer(s) of Python decided it should be. And while most people accept that a tab represents 4 spaces (8 in the early days of Unix), there is nothing to say that is what any coder actually means it to be. – OldBoy Commented Mar 8 at 13:31
  • 3 If you have two lines of code, one with a tab and one with 4 spaces, what is the relative indentation level of those two lines? Are they at the same level (4 spaces per tab), or one level apart (2 spaces), or three levels apart (1 space)? The interpreter can't tell. – John Gordon Commented Mar 8 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 6

quoting from Guido van Rossum (the creator of python)

IIRC when we decided to add a check for inconsistent indentation, the intention was that the parser would not be relying on the equivalence of a tab to any fixed number of spaces at all. The intended abstraction was that mixing of tabs and spaces was only valid if the indentation “levels” would be calculated unambiguously regardless of how many spaces are equivalent to a tab

Tabs and spaces have varying spacing depending on your text editor, it is wrong to assume that a tab is 4 spaces, 2 or 8 spaces per tab are not uncommon. the parser doesn't know what your text editor is showing you.

def func():
    if False:
        print("hello")
    print("world") # uses tabs

if the 4th line print("world") is using tabs and all the lines above it are using spaces, depending on your text editor you could see

def func():
    if False:
        print("hello")
        print("world") # uses tabs

both would be the same code, just previewed with a different text editor.

I mean, the Python Interpreter should be able to just simply convert each Tab to 4 spaces.

Except the traditional tab stop is 8 spaces not 4. It's what you get in a terminal, or using white-space pre/pre-wrap in an HTML context.

It is also what Python would convert tabs to before TabError was introduced, as you can see if you input the following code in a Python file and run it via Python 2 (whether locally or via an online compiler still compatible with Python 2):

exec("""
if True:
\x20\x20\x20\x20print('spaces')
\tprint('tab')
""")

This will fail with an IndentationError with any space count other than 8.

Is there any ambiguous situation that makes TabError sense?

The reader of the code can have their tab stop set to anything. 4 is traditional in the Python ecosystem (but not required) while 2 is common in Ruby or Javascript for instance.

But since Python accepts it, it knows it and is able to convert Tab to it.

Python not accepting it is what you're complaining about. When tabs and spaces are not mixed, it doesn't even need to convert anything, just emit INDENT and DEDENT tokens when the prefix length increases or decreases.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744895072a4599647.html

相关推荐

  • WHY is there a TabError in Python? - Stack Overflow

    I'm not asking WHAT is TabError nor HOW to fix TabError, I'm asking WHY.I completely underst

    2天前
    80

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信