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