#coding=utf-8 ''' 协程入门 yield 生成器 1.包含yield的函数,则是一个可迭代对象。 利用next方法,取每一次的yield send 2.生产者,消费者行为 3.无需立刻执行,需要时才执行 4.斐波拉切数列的例子 1,1,2,3,5,8,13, for i in fun(13): print i 1 1 2 3 5 8 ''' # a = [1,2,3,4] # for i in a: # print i def test(): x = yield '第一步,他会直接返回哟' print '哈哈,第一次哟%s'%x x = yield '%s,它每次请求,不管是send,还是next,都会往下一直跑到下一个yield,并且,把yield后面的值返回回来哟'%x print '哈哈,第二次哟%s'%x x = yield t = test() print t.next() #print t.next() print t.send('嘻嘻嘻,这样才是赋值给第一个x哟!') print t.send('z这是给第二个赋值哟') #print t.send('嘻嘻,试下') #print t.send(5) # print type(range(0,5)) # print type(xrange(0,5)) #for i in xrange(0,5): # print i