我们可以从国外的交通指示牌理解
美国交通规则中【baiyield】的意思是
【让道】
1)duYou must yield the right-of-way to all approaching vehicles and pedestrians.
(你必zhi须给所有正要靠近的车辆和行人让dao路)
2)The car turning left must yield the right-of-way to the other car.
(左转车辆应该让其他车辆)
3)You must yield the right-of-way to any vehicle which has entered the intersection on your right or is approaching the intersection from your right.
(你必须给任何驶入你右车道或靠近你右边车道的车让行)
下面是一个无尽的yield, 因为有while 死循环,可以一直调用next或者send
def func1():
while True: # 1
res = yield 4 # 2l 2r
print(res) # 3
f = func1() # 这里获得一个`generator object`
next(f)
next(f)
print(next(f))
第1个next(f):
第2个next(f):
第3个next(f):
def func1():
total = 0
while True:
yield total
total += 1
g = func1()
print(next(g))
print(next(g))
print(next(g))
打印 0,1,2
def func1():
total = 0
while True:
res = yield total
total += res
g = func1()
g.send(None)
g.send(1)
g.send(2)
输出 0 1 3
注意:
function func1() {
var i = 10
function func2() {
i = i + 1;
return i;
}
return func2;
}
var fun = func1()
console.log(fun())
console.log(fun())
console.log(fun())
执行结果:
11 12 13
yield 和 闭包是类似的, 看js的比较容易看,因为内部一个特殊的变量。 可以说闭包函数就是有状态的函数
原文:https://www.cnblogs.com/hustcpp/p/13157544.html