代码拉取完成,页面将自动刷新
"""
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。