为什么这个python缩进没有抛出错误?

时间:2016-09-19 17:58:03

标签: python coding-style indentation pep8 pep

我们最近在我们的python脚本中发现了一个错误,并由一位同事在其他人的系统上修复(测试,提交和推送)。当时,似乎什么都没有。但是,当我在emacs中启动脚本时,我注意到缩进不正确。以下是违规行:

$ cat -T -E poc.py | grep -nC 3 -F 'up_con not in check'
448-                        up_cons = [up_con[0].encode('utf-8') for up_con in up_cons if up_con and up_con[0]]$
449-^I^I    logger.debug("Got parent concepts %s", up_cons)$
450-                    for up_con in up_cons:$
451:^I^I^Iif up_con != con and up_con not in check_cons:$
452-                            check_cons.append(up_con)$
453-                        idx_con_map[i][up_con] = 1$
454-$
  

那些不知道的人,^I意味着一个硬标签,$暗示着   最终的线。

对此脚本运行测试导致当时输出正确。当我注意到缩进级别的混音时,我自然认为那些测试在某种程度上是错误的。但是,将缩进修复回4个空格规则:

$ cat -T -E poc.py | grep -nC 3 -F 'up_con not in check'
448-                        up_cons = [up_con[0].encode('utf-8') for up_con in up_cons if up_con and up_con[0]]$
449-                    logger.debug("Got parent concepts %s", up_cons)$
450-                    for up_con in up_cons:$
451:                        if up_con != con and up_con not in check_cons:$
452-                            check_cons.append(up_con)$
453-                        idx_con_map[i][up_con] = 1$
454-$

导致相同的执行。之前没有抛出任何错误,这次没有观察到程序执行的变化。

我的问题是,为什么?难道python不应该说有不同级别的缩进等吗?

以下是每行的缩进级别:

Line - with 4-spaces - with tabs
448  -     6         -      6
449  -     5         -      3 <- should cause an error
450  -     5         -      5
451  -     6         -      3 <- shouldn't produce correct outputs
452  -     7         -      7
453  -     6         -      6

0 个答案:

没有答案