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]
时间: 2024-09-30 04:36:50