Possible mixed indentation in Python?

跟風遠走 提交于 2019-12-01 22:11:29

问题


Looking at this question, I tried OP's the code on my machine. Here are a text version and a screenshot:

What just happened? This supposed to be a square function, and it is implemented correctly. To be sure, I copy-pasted the code, and tried it again:

Well, I can't see any difference between these versions of square, but only the latter works.

The only reason I can think of is that I may have mixed tabs and spaces, so the return statement is actually indented, and so the loop is executed exactly once. But I could not reproduce it, and it looks like an unbelievable flaw in the interpreter's mixed-indentation-check. So I have two questions, or maybe three:

  1. What do I miss?
  2. If this is a mixed indentation thing, what it may be, exactly?
  3. If this is a mixed indentation thing, why wasn't it caught by the interpreter? Obviously the whole idea of indentation in python (and in general) is to avoid such problems. And it's too important to let such things slip.

回答1:


Easy!

def square(x):
    runningtotal = 0
    for counter in range(x):
        runningtotal = runningtotal + x
<tab>return runningtotal

First, tabs are replaced (from left to right) by one to eight spaces such that the total number of characters up to and including the replacement is a multiple of eight <...>

So this tab at the last line is replace with 8 spaces and it gets into the loop.




回答2:


For Python indenting - a tab is counted as equivalent to 8 spaces

Since people almost never have their tab width set to 8 spaces, it's never a good idea to mix the two.

Like many people, I used to prefer tabs for indenting, but found that it is a constant source of confusion when emailing code or posting in forums, etc. Which is what has happened here

The most common thing these days is to just have the tab key in your editor insert 4 spaces.

The point is that Python has to respect tabs because of backward compatibility, but it's not a good idea to use them anymore.

As mentioned by @Fredrik, there is the -t option from the man page

      -t     Issue a warning when a source file mixes  tabs  and  spaces  for
              indentation  in a way that makes it depend on the worth of a tab
              expressed in spaces.  Issue an error when the  option  is  given
              twice.

Here the return runningtotal has a tab.

$ python -tt
Python 2.7.4 (default, Apr 19 2013, 18:28:01) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def square(x):
...     runningtotal = 0
...     for counter in range(x):
...         runningtotal = runningtotal + x
...     return runningtotal
  File "<stdin>", line 5
    return runningtotal
                      ^
TabError: inconsistent use of tabs and spaces in indentation


来源:https://stackoverflow.com/questions/17076302/possible-mixed-indentation-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!