码迷,mamicode.com
首页 > 编程语言 > 详细

python 迭代器

时间:2014-12-10 19:46:12      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   使用   sp   for   on   div   

迭代器使用方法:

#!/usr/bin/env python
# !-*-coding:utf-8-*-
__author__ = t

class 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 retval

li = [1, 3, 4, 5, 6, 7, 8, 9]
a = anyIter(li)
i = iter(a)
for s in range(13):
    print i.next()

 

python 迭代器

标签:style   blog   io   color   使用   sp   for   on   div   

原文地址:http://www.cnblogs.com/canbefree/p/4156111.html

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