随笔分类 - 迭代器
摘要:一、可迭代对象基于islice方法实现切片操作 二、islice实现实例: print(list(islice(range(1, 10), 2, 7, 2))) 三、自定义切片方法实现实例: def my_islice(iterable, start, end, step=1): tmp = 0 f
阅读全文
摘要:一、正向迭代器实现__iter__方法 二、反向迭代器实现__reversed__方法 三、实例: from decimal import Decimalclass FloatRange: def __init__(self, a, b, step): self.a = Decimal(str(a)
阅读全文
摘要:一、生成器既是迭代器对象也是可迭代对象 二、函数中含有yield关键字,即是生成器函数 三、自定义实现生成器进行for循环实例: class WeatherIterable(Iterable): def __init__(self, cities): self.cities = cities sel
阅读全文
摘要:一、可迭代对象实现__iter__方法,返回迭代器对象 二、迭代器对象实现__iter__方法,返回迭代器对象,实现__next__方法,进行迭代操作 三、for循环执行的底层实现:首先调用可迭代对象的iter方法生成迭代器对象,再循环调用迭代器对象的next方法,直到抛出StopIteration
阅读全文
浙公网安备 33010602011771号