提供以下翻译会话:
>>> def func(depth,width):
... if (depth!=0):
... for i in range(width):
... print(depth,i)
... func(depth-1,width)
File "<stdin>", line 5
func(depth-1,width)
^
TabError: inconsistent use of tabs and spaces in indentation
有人能告诉我代码中的TabError
是什么吗?
Tags:
TL;DR:不要用选项卡缩进Python代码
在Python 2中,the interpretation of TAB is as if it is converted to spaces using 8-space tab stops;也就是说,每个TAB都将缩进进一步扩展1到8个空格,从而使结果缩进可被8整除。
但是,这不再适用于Python 3—在Python 3 mixing of spaces and tabs is - if not always a SyntaxError - not a good thing to do—simplified[*],制表符只匹配制表符,空格只匹配缩进中的其他空格;即用制表符缩进的块空格空格可能包含用制表符空格制表符缩进的块,但是,如果它包含TABTAB,则会被视为缩进错误,即使块看起来会进一步扩展。
这就是为什么在Python中混合制表符和空格,或者使用制表符缩进at被认为是非常糟糕的做法的原因。
[*]好吧,我确实躺在那里-这没那么简单。Python 3实际上允许在用TABTABTABTAB缩进的块内使用TAB空格缩进的块。从Python documentation:
由于TABTABTABTAB缩进比TAB空格空格更深,因此即使制表符只有一个空格宽,实际上也允许缩进。不过,这太神秘了,你最好还是忘记它,相信我上面说的话。。。或者甚至认为Python根本不允许使用TAB进行缩进。
相关问题 更多 >
编程相关推荐