摘要:
Example1:def fibonacci(): a, b = 0, 1 while True: print 'abc' yield b a, b = b, a+bfib = fibonacci()fib.next()[fib.next() for i in range(10)] #感觉这种写法很精妙Example 2: yield 作为表达式 Send 来填充 yield表达式,throw 抛出异常,close抛出GeneratorExit异常def psychologist(): print 'Please tell me your problems' w 阅读全文
摘要:
class MyIterator(object): def __init__(self, step): self.step = step def next(self): if self.step == 0: raise StopIteration self.step -= 1 return self.step def __iter__(self): return self [el for el in MyIterator(4)] 阅读全文
摘要:
seq = ['one', 'two', 'three']def FormatStr(pos, element): return '%d: %s' % (pos, element)[FormatStr(i, el) for i, el in enumerate(seq)] 阅读全文