Python3 基础教程 - 廖雪峰com/ 112/531 而节省大量的空间。在 Python 中,这种一边循环一边计算的机制,称 为生成器:generator。 要创建一个 generator,有很多种方法。第一种方法很简单,只要把一个 列表生成式的[]改成(),就创建了一个 generator: >>> L = [x * x for x in range(10)] >>> L [0, 1, 4, 9, 16 g <generator objectat 0x1022ef630> 创建 L 和 g 的区别仅在于最外层的[]和(),L 是一个 list,而 g 是一个 generator。 我们可以直接打印出 list 的每一个元素,但我们怎么打印出 generator 的 每一个元素呢? 如果要一个一个打印出来,可以通过 next()函数获得 generator 的下一个 StopIteration 我们讲过,generator 保存的是算法,每次调用 next(g),就计算出 g 的 下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出 StopIteration 的错误。 当然,上面这种不断调用 next(g)实在是太变态了,正确的方法是使用 for 循环,因为 generator 也是可迭代对象: >>> g = (x * x 0 码力 | 531 页 | 5.15 MB | 1 年前3
Tornado 6.1 Documentation
tornado.tcpserver — Basic IOStream-based TCP server Coroutines and concurrency tornado.gen — Generator-based coroutines tornado.locks – Synchronization primitives tornado.queues – Queues for coroutines yield is a generator. All generators are asynchronous; when called they return a generator object instead of running to completion. The @gen.coroutine decorator communicates with the generator via the yield decorator receives a Future from the generator, waits (without blocking) for that Future to complete, then “unwraps” the Future and sends the result back into the generator as the result of the yield expression0 码力 | 931 页 | 708.03 KB | 1 年前3
Tornado 6.0 Documentation
tornado.tcpserver — Basic IOStream-based TCP server Coroutines and concurrency tornado.gen — Generator-based coroutines tornado.locks – Synchronization primitives tornado.queues – Queues for coroutines yield is a generator. All generators are asynchronous; when called they return a generator object instead of running to completion. The @gen.coroutine decorator communicates with the generator via the yield decorator receives a Future from the generator, waits (without blocking) for that Future to complete, then “unwraps” the Future and sends the result back into the generator as the result of the yield expression0 码力 | 869 页 | 692.83 KB | 1 年前3
Python 标准库参考指南 3.6.15 此行为特性的实现将无法正常使用。 34 Chapter 4. 内置类型 The Python Library Reference, 发布 3.6.15 4.5.1 生成器类型 Python 的generator 提供了一种实现迭代器协议的便捷方式。如果容器对象 __iter__() 方法被实现为一 个生成器,它将自动返回一个迭代器对象(从技术上说是一个生成器对象),该对象提供 __iter__() 和 文件或其他对象的快速关闭,以及更方便地操作活 动的十进制算术上下文。除了实现上下文管理协议以外,不同类型不会被特殊处理。请参阅contextlib 模 块查看相关的示例。 Python 的generator 和contextlib.contextmanager 装饰器提供了实现这些协议的便捷方式。如果 使用contextlib.contextmanager 装饰器来装饰一个生成器函数,它将返回一个实现了必要的 Chapter 5. 内置异常 The Python Library Reference, 发布 3.6.15 exception GeneratorExit 当一个generator 或coroutine 被关闭时将被引发;参见 generator.close() 和 coroutine.close()。 它直接继承自BaseException 而不是Exception,因为从技术上来说它并不是一个错误。0 码力 | 1886 页 | 8.95 MB | 9 月前3
Python 标准库参考指南 3.6.15 此行为特性的实现将无法正常使用。 34 Chapter 4. 内置类型 The Python Library Reference, 发布 3.6.15 4.5.1 生成器类型 Python 的generator 提供了一种实现迭代器协议的便捷方式。如果容器对象 __iter__() 方法被实现为一 个生成器,它将自动返回一个迭代器对象(从技术上说是一个生成器对象),该对象提供 __iter__() 和 文件或其他对象的快速关闭,以及更方便地操作活 动的十进制算术上下文。除了实现上下文管理协议以外,不同类型不会被特殊处理。请参阅contextlib 模 块查看相关的示例。 Python 的generator 和contextlib.contextmanager 装饰器提供了实现这些协议的便捷方式。如果 使用contextlib.contextmanager 装饰器来装饰一个生成器函数,它将返回一个实现了必要的 Chapter 5. 内置异常 The Python Library Reference, 发布 3.6.15 exception GeneratorExit 当一个generator 或coroutine 被关闭时将被引发;参见 generator.close() 和 coroutine.close()。 它直接继承自BaseException 而不是Exception,因为从技术上来说它并不是一个错误。0 码力 | 1886 页 | 8.95 MB | 9 月前3
Python 标准库参考指南 2.7.18 . . . . . . . . . . . . . . . . . . . . . . . . 1122 25 开发工具 1123 25.1 pydoc —Documentation generator and online help system . . . . . . . . . . . . . . . . . . . . . . . 1123 25.2 doctest —测试交互性的 a container object’s __iter__() method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) sup- plying the __iter__() and next() methods. More contextlib.contextmanager decorator provide a convenient way to implement these protocols. If a generator function is decorated with the contextlib.contextmanager decorator, it will return a context manager0 码力 | 1552 页 | 7.42 MB | 9 月前3
Python 标准库参考指南 2.7.18 . . . . . . . . . . . . . . . . . . . . . . . . 1122 25 开发工具 1123 25.1 pydoc —Documentation generator and online help system . . . . . . . . . . . . . . . . . . . . . . . 1123 25.2 doctest —测试交互性的 a container object’s __iter__() method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) sup- plying the __iter__() and next() methods. More contextlib.contextmanager decorator provide a convenient way to implement these protocols. If a generator function is decorated with the contextlib.contextmanager decorator, it will return a context manager0 码力 | 1552 页 | 7.42 MB | 9 月前3
Python 标准库参考指南 2.7.18 . . . . . . . . . . . . . . . . . . . . . . . . 1122 25 开发工具 1123 25.1 pydoc —Documentation generator and online help system . . . . . . . . . . . . . . . . . . . . . . . 1123 25.2 doctest —测试交互性的 a container object’s __iter__() method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) sup- plying the __iter__() and next() methods. More contextlib.contextmanager decorator provide a convenient way to implement these protocols. If a generator function is decorated with the contextlib.contextmanager decorator, it will return a context manager0 码力 | 1552 页 | 7.42 MB | 9 月前3
Jinja2 Documentation Release 2.10the runtime behavior or certain template features. Usually these are security related. 11 code_generator_class The class used for code generation. This should not be changed in most cases, unless you after another and yield piece for piece. This method basically does exactly that and returns a generator that yields one item after another as unicode strings. It accepts the same arguments as render() jinja2.environment.TemplateStream A template stream works pretty much like an ordinary python generator but it can buffer multiple items to reduce the number of total iterations. Per default the 160 码力 | 148 页 | 475.08 KB | 1 年前3
Mypy 1.10.0+dev DocumentationCallable[[int, float], float] = f def register(callback: Callable[[str], int]) -> None: ... # A generator function that yields ints is secretly just a function that # returns an iterator of ints, so that's class objects [https://peps.python.org/pep-0484/#the-type-of-class-objects]. Generators A basic generator that only yields values can be succinctly annotated as having a return type of either Iterator[YieldType] [https://docs.python.org/3/library/typing.html#typing.Iterable] as the return-type annotation for a generator function, as it lets mypy know that users are able to call next() [https://docs.python.org/3/library/functions0 码力 | 318 页 | 270.84 KB | 1 年前3
共 308 条
- 1
- 2
- 3
- 4
- 5
- 6
- 31













