Python 3 lets you mix spaces and tabs?

I know there are a lot of questions on tabs and spaces, but it seems that unlike PEP 0008 is talking about Python 3, mixing tabs and spaces is not always illegal. In particular, mixing tabs and spaces in one block is illegal, but blocks with spaces and tabbed blocks are allowed in one file.

For example, this raises a TabError in Python 3.4:

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

But it normal:

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

Is it for design?

Edit: The question is not whether tabs are better than spaces. The question is whether Python matches tabs and spaces in a single file.

+5
source share
2 answers

The test for this is quite simple ( 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 in which this error is actually called ( Parser/tokenizer.c , tok_get() ), it looks like it just compares the type of indentation with what was used by the previous line, rather than what is used throughout the file.

The documentation says ( Doc/reference/lexical_analysis.rst , my emphasis)

Indentation is rejected as inconsistent if the source file mixes the tabs and spaces so that the value depends on the value of the bookmark in spaces ; a TabError occurs in this case.

It is possible to mix tabs and spaces if the "blocks" are completely "divided", returning to the indent level of 0; since there can be no confusion regarding the program logic due to the tab width settings. The problem with mixing tabs and spaces in Python is that Python assumes the tab is eight spaces wide, but the programmerโ€™s editor might use something else. For example, the code is as follows:

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

Will be considered like this with tabost 4:

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

This is clearly not what the program does!

But in such cases as:

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

Everything is in order from a logical point of view, no matter how I look at the tabs, it is always clear what logic is. Of course, itโ€™s still a bad idea to mix tabs and spaces in one file, but this is just a stylistic mistake, not a logical mistake; -)

So, to answer the question: judging by this one line in the documentation, I will say that this is by design. However, I cannot find the PEP for this, and this case has not been verified. I would not rely on this that all versions of Python in the future behave the same!

+4
source

Python is trying to segment each small block of code so that while copying and pasting it still works, you should not do everything perfectly. One of the many beauties of python.

PEP 8 is just a convention for the most readable code, and I recommend that you follow it, but you donโ€™t need to

If you want to see what PEP 8 contains, there are several editors who will check your code for violations that are legal in python, but not very pleasant. I am using PyCharm. PyCharm doesn't like it when you use spaces and tabs in the same file, and it will be snarky with swap paging.

+1
source

Source: https://habr.com/ru/post/1245242/


All Articles