【发布时间】:2019-12-04 16:28:35
【问题描述】:
考虑这段代码:
from random import randrange
from contextlib import suppress
mybiglist = [randrange(1, 100) for i in range(1000)]
count = 0
with suppress(ValueError):
while True:
mybiglist.remove(1)
count += 1
print("Count of 1's is : %d "% count)
我没有放任何 break 语句来结束这个循环。 我无法理解这个“while True”循环如何以及为什么会终止? 当它看到没有更多匹配的元素要删除时,它会神奇地中断! 怎么样?
例如:
from random import randrange
from contextlib import suppress
mybiglist = [randrange(1, 100) for i in range(1000)]
count = 0
with suppress(ValueError):
while True:
mybiglist.remove(222) # a number outside of the randrange, so zero occurrence
count += 1
print("Count of 222's is : %d "% count)
正确打印
Count of 222's is : 0
考虑到计数甚至没有达到值“1”,很明显 list.remove() 导致 while 循环中断。
但 list.remove 的文档只是说明:
list.remove(x) :从列表中删除值等于 x 的第一项。如果没有这样的项目,它会引发 ValueError。
而且我已经抑制了 ValueError。那么这里发生了什么?
以下变体没有抑制确实按预期工作并最终进入无限循环。
from random import randrange
mybiglist = [randrange(1, 100) for i in range(1000)]
count = 0
while True:
try:
mybiglist.remove(222)
count += 1
except ValueError:
pass
print("Count of 222's is : %d "% count)
【问题讨论】:
-
suppress在破坏while后实际上会捕获异常,但您的try, except仍会在while内捕获异常,因此它会继续。
标签: python python-3.x list suppress