5 Star 4 Fork 0

Gitee 极速下载 / python-patterns

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/faif/python-patterns
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
iterator.py 845 Bytes
一键复制 编辑 原始数据 按行查看 历史
Abe 提交于 2022-07-19 14:54 . add type hints
"""
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
Implementation of the iterator pattern with a generator
*TL;DR
Traverses a container and accesses the container's elements.
"""
def count_to(count: int):
"""Counts by word numbers, up to a maximum of five"""
numbers = ["one", "two", "three", "four", "five"]
yield from numbers[:count]
# Test the generator
def count_to_two() -> None:
return count_to(2)
def count_to_five() -> None:
return count_to(5)
def main():
"""
# Counting to two...
>>> for number in count_to_two():
... print(number)
one
two
# Counting to five...
>>> for number in count_to_five():
... print(number)
one
two
three
four
five
"""
if __name__ == "__main__":
import doctest
doctest.testmod()
Python
1
https://gitee.com/mirrors/python-patterns.git
git@gitee.com:mirrors/python-patterns.git
mirrors
python-patterns
python-patterns
master

搜索帮助