yield 实现range()函数

早过忘川 提交于 2019-11-27 05:48:10
def range(*args,step= 1):
    args = list(args)
    if len(args) == 2:
        yield args[0]
        while args[0]<args[1]-1:
            args[0] +=step
            yield args[0]
    elif len(args) == 1:
        count = 0
        yield count
        while count < args[0]-1:
            count += 1
            yield count


for i in range(10):
    print(i)

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe "D:/python/8.9/03 字典生成式.py"
0
1
2
3
4
5
6
7
8
9
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!