为什么在if / else中执行pass语句?

时间:2013-11-25 06:00:47

标签: python if-statement

当我在调试模式下运行以下if / else

if True:
  print 'here'
else:
  print 'there'
  pass  # breakpoint here

调试器在pass语句上停止。为什么pass语句被执行?我知道pass无关紧要,但它在else内。

我在Pycharm 2.7.3上运行python 2.7.5

更新

如果pass语句是程序的最后一行,并且有一个断点,则调试器将停在该pass语句处。我知道它已停止,因为我可以看到当前的堆栈跟踪和变量。

但如果pass不是最后一行,则调试器不会就此停止。

1 个答案:

答案 0 :(得分:6)

调试器不会在pass语句中断。您可以通过在其后面添加一个语句来验证这一点:

$ cat test.py
if True:
  print 'here'
else:
  print 'there'
  pass  # breakpoint here
print 'done'
$ python -m pdb test.py
> test.py(1)<module>()
-> if True:
(Pdb++) list
  1  -> if True:
  2       print 'here'
  3     else:
  4       print 'there'
  5       pass  # breakpoint here
  6     print 'done'
[EOF]
(Pdb++) break 5
Breakpoint 1 at test.py:5
(Pdb++) continue
here
done
The program finished and will be restarted

调试器可能出现以打破那里,因为它是文件中的最后一行?