简体   繁体   English

Python 3允许混合空格和制表符?

[英]Python 3 allows mixing spaces and tabs?

I know there are many questions on tabs vs. spaces, but it appears that, contrary to what PEP 0008 says about Python 3, mixing tabs and spaces is not always illegal. 我知道标签与空格有很多问题,但看起来,与PEP 0008关于Python 3的说法相反,混合标签和空格并不总是非法的。 Specifically, mixing tabs and spaces in the same block is illegal, but blocks with spaces and blocks with tabs are allowed in the same file. 具体来说,在同一块中混合制表符和空格是非法的,但在同一文件中允许带有空格的块和带有制表符的块。

For example, this throws a TabError on Python 3.4: 例如,这会在Python 3.4上引发TabError:

for x in range(10):
    print(x)  # Spaces
    print(x)  # Tab

But this runs fine: 但这很好:

for x in range(10):
    print(x)  # Spaces

for y in range(5):
    print(y)  # Tab

Is this by design? 这是设计的吗?

Edit: The question is not whether tabs are better than spaces. 编辑:问题不是标签是否比空格更好。 The question is whether Python's allowing tabs and spaces in the same file is by design. 问题是Python是否允许同一文件中的制表符和空格是设计的。

The test for this is pretty simple ( Lib/tests/test_exceptions.py ): 对此的测试非常简单( Lib/tests/test_exceptions.py ):

  self.raise_catch(TabError, "TabError") try: compile("try:\\n\\t1/0\\n \\t1/0\\nfinally:\\n pass\\n", '<string>', 'exec') except TabError: pass else: self.fail("TabError not raised") 

Looking at the code where this error is actually thrown ( Parser/tokenizer.c , tok_get() ) it looks like this merely compares the indentation kind to what the previous line used, and not what is used throughout the file. 查看实际抛出此错误的代码( Parser/tokenizer.ctok_get() ),看起来这只是将缩进类型与一行使用的内容进行比较,而不是整个文件中使用的内容。

The documentation says ( Doc/reference/lexical_analysis.rst , emphasis mine) 文档说( Doc/reference/lexical_analysis.rst ,强调我的)

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces ; 如果源文件以一种使得含义依赖于空格制表符的价值的方式混合制表符和空格则缩进被拒绝为不一致; a TabError is raised in that case. 在这种情况下会引发TabError

It's okay to mix tabs and spaces if the "blocks" are completely "separated" by going back to indentation level 0; 如果“块”通过返回到缩进级别0完全“分离”,则可以混合制表符和空格; as there can be no confusion about the program's logic due to tab width settings. 因为标签宽度设置,不能混淆程序的逻辑。 The problem with mixing tabs and spaces in Python is that Python assumes that a tab is eight spaces wide, but that the programmer's editor may use something else. 在Python中混合制表符和空格的问题是Python 假设一个制表符是八个空格宽,但程序员的编辑器可能使用其他东西。 For example, code like this: 例如,代码如下:

def my_fun(i):
    if i == 6:
        foo()
------->bar()  # Tab

Will be seen as this with a tabstop of 4: 将用4的tabstop看作这个:

def my_fun(i):
    if i == 6:
        foo()
    bar()

Which is obviously not what the program does! 这显然不是该计划的作用!

But in cases like: 但在以下情况下:

def my_fun(i):
    if i == 6:
        foo()
        bar()

def my_fun2(i):
--->if i == 7:
--->--->foo()
--->--->bar()

It's okay from a "logic" point of view as no matter how I view tabs, it's always clear what the logic is. 从“逻辑”的角度来看,没关系,无论我如何查看标签,它总是清楚逻辑是什么。 It's of course still a bad idea to mix tabs and spaces in a single file, but that's merely a stylistic error, and not a logic error ;-) 在单个文件中混合制表符和空格当然一个坏主意,但这只是一个风格错误,而不是逻辑错误;-)

So to answer the question: judging from that one line in the documentation, I would say that this is by design. 所以回答这个问题:从文档中的那一行来看,我会说这是设计的。 I can't find a PEP for this, though, and this case isn't tested. 但是,我找不到这个PEP,并且这个案例没有经过测试。 I would be not rely on it that all Python versions in the future behave the same! 我不会依赖它,以后所有 Python版本的行为都一样!

Python tries to segment each little block of code so that when you copy and paste, it still works, and you aren't forced to make everything perfect. Python试图对每一小块代码进行分段,这样当你复制和粘贴时,它仍然有效,而且你不会被迫使一切都完美无缺。 One of the many beauties of python. python的众多美女之一。

PEP 8 is just a convention for the most readable code, and I recommend that you do follow it, but you don't have to PEP 8仅仅是最可读的代码是一个约定,我建议你遵循它,但你不必

If you want to look for what PEP 8 encompasses there are several editors that will inspect your code for violations that are legal in python, but not nice. 如果你想查找PEP 8所包含的内容,有几个编辑器会检查你的代码是否存在python中合法的违规行为,但不是很好。 I use PyCharm. 我用PyCharm。 PyCharm doesn't like when you use spaces and tabs in the same file and it will get snarky with the squiggle under-linings. 当你在同一个文件中使用空格和制表符时,PyCharm不喜欢它,并且它会因为波浪底线而变得狡猾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM