标签: python generator yield-return
通常人们在循环中使用并尝试以下代码工作,并想知道总和杠杆率如何?
# a generator that yields items instead of returning a list def firstn(n): num = 0 while num < n: yield num num += 1 sum_of_first_n = sum(firstn(1000000)) print sum_of_first_n