Discuss / Python / 两个例子,一个需要生成generator对象,一个不需要。使用迭代时候没有生成generator对象,迭代的是谁呢?

两个例子,一个需要生成generator对象,一个不需要。使用迭代时候没有生成generator对象,迭代的是谁呢?

Topic source

小崔鹏

#1 Created at ... [Delete] [Delete and Lock User]

def odd(): print('step 1:') yield 1 print('step 2:') yield 3
print('step 3:') yield 5 o = odd() print(next(o)) print(next(o)) print(next(o))

这个需要生成generator对象

def fibonacci(max): n,a,b = 0,0,1 while n < max: yield b

    a,b = b,a + b
    n = n + 1

for n in fibonacci(6): print(n) 这个没有生成对象 问题是第二个是如何迭代的呢?迭代的是什么内容呢?


  • 1

Reply