yield 实现range()函数

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:UsersAdministratorAppDataLocalProgramsPythonPython36python.exe "D:/python/8.9/03 字典生成式.py"
0
1
2
3
4
5
6
7
8
9
原文地址:https://www.cnblogs.com/oxtime/p/11348150.html