1
考虑下面的代码片段(注意我用的是全球的,因为外地的关键字不可用在Python 2.7)生成表达式失败,最大递归深度
def foo(L,K):
global count
count = 0
def bar(f,L):
global count
for e in L:
if e - f == K or f - e == K: count += 1
yield e
try:
while True:
L = bar(L.next(),L)
except StopIteration:
return count
count=0
print foo((int(e) for e in some_string.split()),some_number)
其中
some_string: A space delimited integers
some_number: An integer
时len(some_string) = 4000
,上述代码失败,出现错误
RuntimeError: maximum recursion depth exceeded while calling a Python object
难道是因为se内部嵌套的生成器被实现为递归?
+1 - 比我更好的分析 – 2012-08-05 16:27:45