首页 > 代码库 > python 迭代器

python 迭代器

迭代器使用方法:

#!/usr/bin/env python# !-*-coding:utf-8-*-__author__ = tclass anyIter(object):    def __init__(self, li):        self.li =iter(li)    def __iter__(self):        return self    #重写了 iter 中的next方法.    def next(self, howmany=1):        retval = None        for eachItem in range(howmany):            try:                retval = self.li.next()            except Exception, e:                break        return retvalli = [1, 3, 4, 5, 6, 7, 8, 9]a = anyIter(li)i = iter(a)for s in range(13):    print i.next()

 

python 迭代器