You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Python斐波那契生成器每轮迭代逻辑的技术咨询

Understanding Your Fibonacci Generator's Iterations, Line by Line

Hey there! I totally get wanting to nail down every detail before moving on—let's walk through this generator step by step so you can see exactly what's happening in each iteration.

First, here's your code again for reference:

def fibonacci_generator() :
    a , b = 0 , 1
    while True :
        yield a
        a , b = b , a + b

fib = fibonacci_generator()
for i in fib :
    if i > 100 :
        break
    else :
        print( 'Generated:' , i )

Let's start from the very beginning and track every change to a and b:

Initialization

When you run fib = fibonacci_generator(), the function starts executing:

  • It sets a = 0 and b = 1 in one line (parallel assignment).
  • It hits the while True loop, then immediately reaches yield a. This pauses the function and "sends" the value 0 back to the caller—but the function remembers its state (the values of a and b are still 0 and 1).

Iteration 1 (First print: 0)

  • The for loop grabs the first yielded value (0). Since 0 ≤ 100, it prints Generated: 0.
  • Now the generator resumes right after the yield statement. It runs a, b = b, a + b:
    • The right-hand side calculates first: b is 1, and a + b is 0 + 1 = 1.
    • Both values are assigned at the same time: a becomes 1, b becomes 1.
  • The generator loops back to while True and pauses at yield a again, now holding a = 1.

Iteration 2 (Second print: 1)

  • The for loop gets the next value (1), prints Generated: 1.
  • Generator resumes, runs a, b = b, a + b:
    • Right-hand side: b is 1, a + b is 1 + 1 = 2.
    • Now a = 1, b = 2.
  • Pauses at yield a with a = 1.

Iteration 3 (Third print: 1)

  • Loop grabs 1, prints it.
  • Generator updates: a takes b's value (2), b takes 1 + 2 = 3. Now a = 2, b = 3.
  • Pauses at yield a with a = 2.

Iteration 4 (Fourth print: 2)

  • Loop gets 2, prints it.
  • Generator updates: a = 3, b = 2 + 3 = 5.
  • Pauses at yield a with a = 3.

Skipping Ahead to the Final Iterations

This pattern continues: each time the generator resumes, it updates a and b using their previous values (thanks to parallel assignment, we don't overwrite a before calculating the new b).

Let's jump to the last few steps before the loop breaks:

  • When a = 89, the loop prints Generated: 89.
  • Generator updates: a = b = 144, b = 89 + 144 = 233.
  • Next, the for loop grabs 144, which is greater than 100. The loop hits break and stops.

Key Takeaway

The magic here is yield—it lets the function pause and resume, keeping track of its state between calls. The parallel assignment a, b = b, a + b is crucial too: it ensures we use the original values of a and b to calculate the next pair, instead of overwriting a first and messing up the calculation.

内容的提问来源于stack exchange,提问作者combatwombat833

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:35:55