【python 生成器 面试必备】yield关键字,协程必知必会系列文章--自己控制程序调度,体验做上帝的感觉 2

这篇文章要解决的问题:How to Pass Value to Generators Using the "yield" Expression in Python

ref:https://python.plainenglish.io/yield-python-part-ii-e93abb619a16

1.如何传值

yield 是一个表达式!!!!

yield is an expression! It is used just like an expression would:

received = yield 'hello'

In this case value of received is hello .

received = yield

Here, the value of received is None .

However, it's been suggested to use the expression form sparingly also because of confusion.

但是也有人建议少用,因为容易引起混淆!

2.yield 生成器的几种状态

···python

from inspect import getgeneratorstate

复制代码
def gen_echo():
    while True:
        received = yield
        print(f'Inside method: {getgeneratorstate(echo)}')
        print(f'You said: {received}')


echo = gen_echo()
print(f'After generator creation: {getgeneratorstate(echo)}')
print(f'First call value to generator: {next(echo)}')
print(f'After first call to generator: {getgeneratorstate(echo)}')

# sending value to generator
echo.send('hello')

print(f'After sending the value: {getgeneratorstate(echo)}')

···

bash 复制代码
C:\Users\HP\.conda\envs\torch1.8\python.exe D:/code/python_project/01-coroutine-py-mooc/8/demo_ccc.py
After generator creation: GEN_CREATED
First call value to generator: None
After first call to generator: GEN_SUSPENDED
Inside method: GEN_RUNNING
You said: hello
After sending the value: GEN_SUSPENDED

3.原理图

其实就是线程【用户级线程=也叫协程】的状态转换图!!!

3.协程练习

3.1 原始方式

python 复制代码
def running_averager():
    total = 0
    count = 0
    running_average = None
    while True:
        value = yield running_average
        total += value
        count += 1
        running_average = total/count

averager = running_averager() # generator creation
next(averager) # priming
print(averager.send(10))
print(averager.send(20))
print(averager.send(30))

3.2 闭包【装饰器】方式

3.1相同的功能代码再用闭包的方式实现一遍,After priming, the generator is on suspend state, after which we can send data to a generator and get running_average.

Since we have to do that extra step of next() to generator, why not have it automated. We have a decorator to do that.

python 复制代码
def prime(generator_fun):
    '''
    Method to prime generator function
    '''
    generator = generator_fun()
    next(generator)
    return generator

def running_averager():
    '''
    calculates average of values sent
    '''
    total = 0
    count = 0
    running_average = None
    while True:
        value = yield running_average
        total += value
        count += 1
        running_average = total/count

iterable = [10, 11, 12, 13, 14, 15]
averager = prime(running_averager) # decorating

for value in iterable:
    running_average = averager.send(value)
    print(running_average)

输出:

复制代码
10.0
10.5
11.0
11.5
12.0
12.5

4.总结

In summary, yield is an expression too and we can send value to a generator when it is in suspend state. The first suspend state is achieved by priming the generator.

相关推荐
qq_336313931 分钟前
javaweb-web基础(springboot入门)
java·开发语言·mysql
玄同76522 分钟前
LangChain 1.0 模型接口:多厂商集成与统一调用
开发语言·人工智能·python·langchain·知识图谱·rag·智能体
特立独行的猫a26 分钟前
C++轻量级Web框架介绍与对比:Crow与httplib
开发语言·前端·c++·crow·httplib
fie888928 分钟前
基于C#的推箱子小游戏实现
开发语言·c#
喵手36 分钟前
Python爬虫实战:构建招聘会数据采集系统 - requests+lxml 实战企业名单爬取与智能分析!
爬虫·python·爬虫实战·requests·lxml·零基础python爬虫教学·招聘会数据采集
菜鸟小芯36 分钟前
Qt Creator 集成开发环境下载安装
开发语言·qt
阿猿收手吧!1 小时前
【C++】引用类型全解析:左值、右值与万能引用
开发语言·c++
「QT(C++)开发工程师」1 小时前
C++ 策略模式
开发语言·c++·策略模式
专注VB编程开发20年1 小时前
python图片验证码识别selenium爬虫--超级鹰实现自动登录,滑块,点击
数据库·python·mysql
iFeng的小屋1 小时前
【2026最新当当网爬虫分享】用Python爬取千本日本相关图书,自动分析价格分布!
开发语言·爬虫·python