• 技术文章 >Python技术 >Python基础教程

    python中yield from怎么用?

    十一十一2021-03-05 09:56:25原创4459

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    yield from本质:

    for item in iterable: yield item的缩写版

    代码示例:

    1

    2

    3

    4

    def g(x):

        yield from range(x , 0, -1)

        yield from range(x)

    print(list(g(5)))

    利用yield from语句向生成器(协程)传送数据

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    def writer():

        while True:

            w = (yield)

            print('>>', w)

    def writer_wrapper(coro1):

    coro1.send(None)

        while True:

            try:

                x = (yield)

                coro1.send(x)

            except StopIteration:

                pass

    def writer_wrapper(coro2):

    yield from coro2

    专题推荐:yieldfrom怎么用
    上一篇:python中的插入排序怎么用? 下一篇:新手学习python2还是python3?详细区别讲解

    相关文章推荐

    • python中elasticsearch是什么?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网