关于list.extend(iterable)

时间:2015-08-10 19:49:08   收藏:0   阅读:299

extend内的参数只要是iterable就可以,那么也可以添加定制的iterable,开整。

class A(object):
        def __init__(self):
                self.a = 0
        def __iter__(self):
                return self
        def next(self):
                self.a = self.a + 1
                if self.a > 10:
                        self.a = 0
                        raise StopIteration();
                return self.a

  

>>> import collections
>>> isinstance(a, collections.Iterable)
True
>>> import h
>>> a = h.A()
>>> for x in a:
...     print x,
... 
1 2 3 4 5 6 7 8 9 10
>>> l = list(‘sdfsd‘)
>>> l
[‘s‘, ‘d‘, ‘f‘, ‘s‘, ‘d‘]
>>> l.extend(a)
>>> l
[‘s‘, ‘d‘, ‘f‘, ‘s‘, ‘d‘, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

  

  

原文:http://www.cnblogs.com/iNeoWong/p/4719021.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!