def yanghui():
L = [1]
print(L)
while True:
yield L
L=[1]+[L[i]+L[i+1] for i in range(len(L)-1)]+[1]
print(L)
pass
n=0
for m in yanghui():
n=n+1
if n==5:
break
因为遇到yield语句返回,此时print [1]
再次执行时从上次返回的yield语句处继续执行
此时L=[1],len(L)=1
L=[1]+[L[i]+L[i+1] for i in range(0)]+[1]
[L[i]+L[i+1] for i in range(0)] (因为range(0)所以不计算)
L=[1]+[1]
当 L=[1]+[1],len(L)=2
L=[1]+[L[i]+L[i+1] for i in range(1)]+[1]
[L[i]+L[i+1] for i in range(1)]=[L[0]+L[1]]=[2]
L=[1]+[2]+[1]
当 L=[1]+[2]+[1],len(L)=3
L=[1]+[L[i]+L[i+1] for i in range(2)]+[1]
[L[i]+L[i+1] for i in range(2)]=[L[0]+L[1]]=[3]
=[L[1]+L[2]]=[3]
L=[1]+[3]+[3]+[1]
当 L=[1]+[3]+[3]+[1],len(L)=4
L=[1]+[L[i]+L[i+1] for i in range(3)]+[1]
[L[i]+L[i+1] for i in range(3)]=[L[0]+L[1]]=[4]
=[L[1]+L[2]]=[6]
=[L[2]+L[3]]=[4]
L=[1]+[4]+[6]+[4]+[1]