关于list.extend(iterable)

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]

  

  

posted on 2015-08-10 19:18  sudo987  阅读(425)  评论(0编辑  收藏  举报

导航