码迷,mamicode.com
首页 > 其他好文 > 详细

yield 实现range()函数

时间:2019-08-13 20:48:17      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:int   ogr   list   min   ams   python3   for   mini   while   

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

yield 实现range()函数

标签:int   ogr   list   min   ams   python3   for   mini   while   

原文地址:https://www.cnblogs.com/oxtime/p/11348150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!