摘要: 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 阅读全文
posted @ 2012-04-09 16:24 谷安 阅读(160) 评论(0) 推荐(0) 编辑
摘要: 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)] 阅读全文
posted @ 2012-04-09 11:23 谷安 阅读(308) 评论(0) 推荐(0) 编辑
摘要: seq = ['one', 'two', 'three']def FormatStr(pos, element): return '%d: %s' % (pos, element)[FormatStr(i, el) for i, el in enumerate(seq)] 阅读全文
posted @ 2012-04-09 10:30 谷安 阅读(167) 评论(0) 推荐(0) 编辑