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:\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

猜你喜欢

转载自www.cnblogs.com/oxtime/p/11348150.html