【发布时间】:2020-10-04 19:54:20
【问题描述】:
在以下代码中,a 和 b 都是生成器函数的输出,并且可以计算为 None 或具有值。
def testBehaviour(self):
a = None
b = 5
while True:
if not a or not b:
continue
print('blaat')
如果我在continue 语句所在的行和print 语句所在的行上放置断点(使用Visual Studio Code),则两者都不会命中。 print 语句没有被调用,循环只是按预期无限期地运行,但我希望断点被命中。
如果我将代码更改为以下之一:
def testBehaviour(self):
a = None
b = 5
while True:
if not a:
continue
print('blaat')
或:
def testBehaviour(self):
a = None
b = 5
while True:
if not a or not b:
c = 'dummy'
continue
print('blaat')
再次在continue 和print 语句的行上放置断点,断点被命中。
谁能告诉我为什么没有命中断点?这似乎不仅仅发生在 Visual Studio Code 中,因为我们的代码覆盖工具还指出 continue 语句未被调用。
这是在 32 位 Windows 7 上的 python 2.7 上。
【问题讨论】:
-
您可以通过查看为不同版本的函数生成的字节码获得线索——您可以通过以下方式查看:
dis.dis(testBehaviour)。 -
@martineau 感谢您的建议!在字节码中我也没有看到太大的区别,它们都使用
POP_JUMP_IF_FALSE、JUMP_ABSOLUTE和JUMP_FORWARD。你知道要寻找哪些具体的东西吗? -
这是python-3打印语句
-
@rioV8
print('Whatever')也是有效的 Python 2.7 代码......但似乎无关紧要的语句类型。
标签: python python-2.7 visual-studio-code