当前位置: 首页 > news >正文

手机登录网站后台王也最后结局

手机登录网站后台,王也最后结局,wordpress按钮,使用wordpress编辑demo_yield.py 程序主要演示了microPython中生成器(Generator)的使用,特别是通过yield语句进行双向通信的机制(既能产出值也能接收值),以及控制流的暂停和恢复机制。常见的普通 return 函数会立即返回所有结…

demo_yield.py 程序主要演示了microPython中生成器(Generator)的使用,特别是通过yield语句进行双向通信的机制(既能产出值也能接收值),以及控制流的暂停和恢复机制。

常见的普通 return 函数会立即返回所有结果,而使用 yield 的函数会在每次迭代时逐步产生结果。

def event():print('start yield')# return next(task) and yield next(task.send('set_two'))one = yield 'get_one'assert(one == 'set_two')print(one)yield 'get_two'  # return next(task) and yield next(task.send('set_two'))print('exit yield')yield  # yield next() to exit or raise StopIterationtask = event()
run_one = next(task)  # need next(task) init and next(task) == task.send(None)
# so next(task) => yield 'get_one' => run_one = 'get_one'
assert(run_one == 'get_one')
run_two = task.send('set_two')
assert(run_two == 'get_two')
print('run : ', run_one, ' and ', run_two)try:next(task)print('run end')next(task)  # will raise StopIteration
except Exception as e:print('yield StopIteration')if __name__ == '__main__':def task():while True:print('hello')yieldtmp = task()while True:next(tmp)while True:print('hello')

当不做任何修改,执行示例程序时,实际输出结果只有循环的 hello。

让我们来修改以下,注释掉if __name__ == '__main__':这句及以后面的程序,再运行一下:

def event():# event函数是一个生成器函数,因为它包含yield语句。当调用event()时,它会返回一个生成器对象,而不会立即执行函数体内的代码。print('start yield')# return next(task) and yield next(task.send('set_two'))one = yield 'get_one'# 第一次暂停,返回'get_one'assert(one == 'set_two')# 验证传入的值print(one)# 打印接收到的值yield 'get_two'  # # 第二次暂停,返回'get_two'   return next(task) and yield next(task.send('set_two'))print('exit yield')yield  # yield next() to exit or raise StopIteration  # 最后一次暂停,返回None#这个函数包含三个yield语句,每次执行到yield时会暂停并返回一个值。task = event()#创建生成器对象(此时函数并未执行)
run_one = next(task)  ## 启动生成器,执行到第一个yield处暂停    need next(task) init and next(task) == task.send(None)
# so next(task) => yield 'get_one' => run_one = 'get_one'
assert(run_one == 'get_one')
run_two = task.send('set_two')# 发送值'set_two'到生成器,恢复执行
assert(run_two == 'get_two')
print('run : ', run_one, ' and ', run_two)try:next(task)print('run end')next(task)  # will raise StopIteration
except Exception as e:print('yield StopIteration')#if __name__ == '__main__':#    def task():
#        while True:
#            print('hello')
#            yield#    tmp = task()
#    while True:
#        next(tmp)#    while True:
#        print('hello')

修改后的程序运行结果如下:

yield 是定义 生成器函数(generator function) 的关键字。通过以上程序也可以看出来:yield 的作用就是把一个函数变成一个 生成器 generator,带有 yield 的函数Python 解释器会将其认为是一个生成器 generator,函数内部的代码执行到 yield时,函数就返回一个迭代值,下次迭代时,代码从 yield 的下一条语句继续执行,直到再次遇到 yield。

    1. 调用生成器函数不会立即执行,而是返回一个生成器对象。

    2. 必须使用next()send(参数)启动生成器。

    3. 当需要逐个返回大量数据项时,优先使用 yield,可以很好的节约内存使用量

    4. 在构建数据处理流水线时,结合 yield 和函数组合可进行高效的处理。

    start yield
    set_two
    run :  get_one  and  get_two
    exit yield
    run end
    yield StopIteration
    MPY: soft reboot
    CanMV v1.3-132-gb19c756(based on Micropython e00a144) on 2025-08-09; k230_canmv_01studio with K230

    结合每步的输出,加详细注释如下:

    def event():# event函数是一个生成器函数,因为它包含yield语句。当调用event()时,它会返回一个生成器对象,而不会立即执行函数体内的代码。print('start yield')# return next(task) and yield next(task.send('set_two'))one = yield 'get_one'# 第一次暂停,返回'get_one'assert(one == 'set_two')# 验证传入的值print(one)# 打印接收到的值yield 'get_two'  # # 第二次暂停,返回'get_two'   return next(task) and yield next(task.send('set_two'))print('exit yield')yield  # yield next() to exit or raise StopIteration  # 最后一次暂停,返回None#这个函数包含三个yield语句,每次执行到yield时会暂停并返回一个值。task = event()#创建生成器对象(此时函数并未执行)
    run_one = next(task)  # 实际输出:start yield  (启动生成器,执行到第一个yield处暂停    need next(task) init and next(task) == task.send(None))
    print(run_one)#实际输出:get_one
    ## so next(task) => yield 'get_one' => run_one = 'get_one'
    assert(run_one == 'get_one')
    run_two = task.send('set_two')# 发送值'set_two'到生成器,恢复执行
    print(run_two)#实际输出:get_two
    assert(run_two == 'get_two')
    print('run : ', run_one, ' and ', run_two)try:next(task)print('run end')next(task)  # will raise StopIteration
    except Exception as e:print('yield StopIteration')#====================================================
    实际输出结果::start yield
    get_one
    set_two
    get_two
    run :  get_one  and  get_two
    exit yield
    run end
    yield StopIteration
    MPY: soft reboot
    CanMV v1.3-132-gb19c756(based on Micropython e00a144) on 2025-08-09; k230_canmv_01studio with K230

    http://www.dtcms.com/a/498528.html

    相关文章:

  • html5炫酷网站天元建设集团有限公司枣庄
  • 用html制作网站流程做网站分为几种
  • wordpress shard网站建设和网站优化的区别
  • php网站开发实例教程第七章网站设计的专业流程
  • 建设网站流程图wordpress演示站功能
  • 建设银行网上银行网站打不开山东百度推广代理
  • 宁波网站建设定制开发东莞seo推广
  • 做外贸网站用哪些小语种中国城乡与住房建设部网站
  • 用什么来网站开发好湛江专业网站制作
  • 网站建设如何缴纳印花税局域网wordpress建站
  • 生活信息网站建设百度seo推广首选帝搜软件
  • 企业建设网站是网络营销吗如何学建设网站首页
  • 做网站产品图片素材中国建设部网站能查叉车证
  • 南京500元做网站怎么把自己做的网站放到百度上
  • 白云怎样优化网站建设天猫交易购买平台
  • 顺德做网站那家好开发区人才
  • 山东seo网站推广网站标题改了
  • 网站制作好吗wordpress 绑定手机版
  • 上海网站排名优化价格饭店的网站建设进行评价
  • wordpress如何搬站赣州经开区最新规划图
  • 美团网站开发费用天润网站建设
  • 查看网站建设时间电商网站运营步骤
  • 有什么网站可以做试题保护环境网页设计教程
  • 附近网站建设服务公司企业建设网站网站建设公司
  • 长沙建站智能模板海外购物电商平台
  • 建电子商务网站多少钱微信营销案例100例
  • 西安做网站的公司电话国内的跨境电商平台有哪些
  • 网站建设新的开始北京网页设计公司
  • 空间设计网站南城网站建设公司案例
  • 世界上做的最好的前端网站企业网站建立策划书