Example 1. Simple # output columns before the Fibonacci number N
# disadvantages: poor reusability of the function, the function returns fab as None, other functions can not be obtained the function to generate the number of columns
# fab to improve the function of the reusable versatility, it is best not to directly print out the number of columns, but returns a List.
Fab1 DEF (max):
n-, A, B = 0, 0,. 1
the while n-<max:
Print (B, End = '')
A, B = B, A + B
n-+. 1 = n-
Fab1 (. 5)
Example # 2
# disadvantages: This function takes in the memory will run as a parameter max increases, if you want to control the memory footprint,
# List best not to use to store intermediate results, but by iterable object iterative
DEF Fab2 (max):
n-, A, B = 0, 0,. 1
L = []
the while n-<max:
L.append (B)
A, B = B, A + B
n-+. 1 = n-
return L
Example # 3
# Description: a function with yield is no longer an ordinary function, Python interpreter will treat this as a Generator,
# calls fab (5) does not perform fab function, but returns an iterable objects!
# When executed for loop, each loop will execute fab internal function code is executed to yield b, fab iterator function returns a value,
# the next iteration, the code continues from the next statement yield b, and local variables and function looks left off before the execution is exactly the same,
# function then continues until it encounters yield again.
DEF Fab3 (max):
n-, A, B = 0, 0,. 1
the while n-<max:
the yield B
# Print B
A, B = B, A + B
n-= n-+. 1
F = Fab3 (. 5)
Print ( " f is an iterative object and does not execute the function ")
Print (f)
Print ( 'Fab3 returns an iterable object values may be obtained for recycling')
for f in n-:
Print (n-)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#例4:
#说明:yield from iterable本质上等于for item in iterable: yield item的缩写版
def f_wrapper1(f):
for
g
in
f:
yield g
wrap = f_wrapper1(fab3(5))
for
i
in
wrap:
print(i,end=
' '
)
print(
'\n使用yield from代替for循环'
)
def f_wrapper2(f):
yield
from
f#注意此处必须是一个可生成对象
wrap = f_wrapper2(fab3(5))
for
i
in
wrap:
print(i,end=
' '
)
print(
'\n---------------------'
)
print(
'yield from包含多个子程序'
)
def g(x):
yield
from
range(x, 0, -1)
yield
from
range(x)
print(list(g(5)))
for
g
in
g(6):
print(g,end=
','
)
print(
'\n---------------------'
) 注意红色部分就是替代的部分,yield
from
iterable本质上等于
for
item
in
iterable: yield item的缩写版
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#例5 利用yield from语句向生成器(协程)传送数据
#传统的生产者-消费者模型是一个线程写消息,一个线程取消息,通过锁机制控制队列和等待,但一不小心就可能死锁。
#如果改用协程,生产者生产消息后,直接通过yield跳转到消费者开始执行,待消费者执行完毕后,换回生产者继续生产,效率极高:
def consumer_work(len):
# 读取send传进的数据,并模拟进行处理数据
print(
"writer:"
)
w=
''
while
True:
w = yield w # w接收send传进的数据,同时也是返回的数据
print(
'[CONSUMER] Consuming %s...>> '
, w)
w*=len #将返回的数据乘以100
time.sleep(0.1)
def consumer(coro):
yield
from
coro#将数据传递到协程(生成器)对象中
def produce(c):
c.send(None)#
"prime"
the coroutine
for
i
in
range(5):
print(
'[Produce] Producing %s----'
, i)
w=c.send(i)#发送完成后进入协程中执行
print(
'[Produce] receive %s----'
, w)
c.close()
c1=consumer_work(100)
produce(consumer(c1))<br><br>执行结果:<br>writer:<br>[Produce] Producing %s---- 0<br>[CONSUMER] Consuming %s...>> 0<br>[Produce] receive %s---- 0<br>[Produce] Producing %s---- 1<br>[CONSUMER] Consuming %s...>> 1<br>[Produce] receive %s---- 100<br>[Produce] Producing %s---- 2<br>[CONSUMER] Consuming %s...>> 2<br>[Produce] receive %s---- 200<br>[Produce] Producing %s---- 3<br>[CONSUMER] Consuming %s...>> 3<br>[Produce] receive %s---- 300<br>[Produce] Producing %s---- 4<br>[CONSUMER] Consuming %s...>> 4<br>[Produce] receive %s---- 400<br><br>yield
from
一般掌握这两种用法即可
|