我们最近在我们的 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