python3 yield send_【Python学习】yield send我就说这么多

C#的yield已经忘得差不多了。又遇到python的yield。

iterator

deftestYield():print 'yield1'm= yield 1

print 'm =', mprint 'yield2'

yield 5

for a intestYield():print 'test'result=testYield()

result.send('test')print list(result)

OUTPUT:

yield1

test

m = None

yield2

test

File "C:\pytest\Sele\Generator.py", line 36, in

result.send('test')

TypeError: can't send non-None value to a just-started generator

[Finished in 0.2s with exit code 1]

参考这里面的说:

http://www.jb51.net/article/15717.htm

send(something) and next()

第一次调用,send不能传非空值,不知道为什么,以后再搞明白

yield 在函数里出现,说明这个函数是generator,生成器,会被区别对待。

举个栗子~

deftestYield():

m= yield 1

printtype(m)printm

m2= yield 5

yield 88result=testYield()printresult.next()print result.send('fighting')

result.next()

print result.next()

# print list(result)

print result.next() A:给我做一块蛋糕, 并打包

哒哒哒,进入函数工厂

计算机: 好的,做好了。给你1

print.send('fighting') B:给我做一块蛋糕,让‘fighting’来做,并打包

哒哒哒,进入函数工厂

计算机:好的,这就让fighting去做。给你5

result.next()C:给我做一块蛋糕, 并打包

哒哒哒,进入函数工厂

计算机:好的,做好了。给你88,哎?人呢?

print result.next() D:给我做一块蛋糕, 并打包

哒哒哒,进入函数工厂

计算机: 没有原料了,做不出来蛋糕了,停止售卖 stop iteration

print list(result) E:看看你们能做啥

哒哒哒,进入函数工厂

计算机:我们空了 输出【】正儿八经的输出是:

1

fighting

5

File "C:\pytest\Sele\Generator.py", line 39, in

print result.next()

StopIteration

result = testYield()

result.next() #启动了这个生成器,直到第一个yield语句 hold

你去取这个 result.next(),比如print result.next(), 这个值才真正生成了,result.next()的type是int

result.send(something), 这个something会被传到yield表达式中,m= 'something'。type(m) is str. print m才能获得你用send传入的值。

说明什么呢?

你用send传什么东西,如果你在方法里并不用,就没什么用了。我暂时是这么想的。

result.send(something)继续寻找下一个yield,并hold

跟next()类似,send也会使得生成器停在那里。

print result.send(something) 出来的并不是something。print的是generator找到的下一个yield的值。

这翻译的生成器,就是生成函数的返回值对吧,yield英文是啥意思?produce生产一个东西

再看一个栗子~

defh():print 'Wen Chuan'm= yield 5 #Fighting!

print 'm=', m

d= yield 12

print 'We are together!'

printd

test= yield 35

printtest

c=h()

current= c.next() #start generator, stop at the first yield

printcurrent

current= c.send('Fighting!') #send current yield 'fighting', stop at the next yield

printcurrent

c.send('got it')printlist(c)print list(h())

输出:

Wen Chuan

5

m= Fighting!

12

We are together!

got it

None

[]

Wen Chuan

m= None next() = send(None)

We are together!

None

None

[5, 12, 35]

【时隔快一年,之前学习的python忘得差不多了。于是打算重新学】

http://anandology.com/python-practice-book/iterators.html

说点正常人能看懂的。之前写的我自己都看不懂。。下面英文是摘抄。

Generators simplifies creation of iterators. A generator is a function that produces a sequence of results instead of a single value.

Each time the yield statement is executed the function generates a new value.

So a generator is also an iterator. You don’t have to worry about the iterator protocol.

The word “generator” is confusingly used to mean both the function that generates and what it generates. In this chapter, I’ll use the word “generator” to mean the genearted object and “generator function” to mean the function that generates it.

Can you think about how it is working internally?

When a generator function is called, it returns a generator object without even beginning execution of the function. When next method is called for the first time, the function starts executing until it reaches yield statement. The yielded value is returned by the next call.

The following example demonstrates the interplay between yield and call to next method on generator object.

好的这段话说得不错,我来翻译一下。

说啊这个Generators是简化iterator的创建的。一个Generator是用来生成一串结果的方法,而不是仅返回一个值。

每次呢,yield语句一旦执行,这个方法就生产出一个新的值。

所以呢,这个generator也是一个迭代器。你就别担心iterator的规则它是否适用,答案是肯定的。

这个词"generator"到底意思是它的作用是生成还是说它生成的东西呢?这一点让人困惑。在俺们这里,我用generator来表示生成的对象,用generator function来表示它生成东西的这个作用。

当一个generator function被调用的时候呢,它会返回一个generator 对象,只是返回个对象,并不干其他的事情,甚至都没开始执行这个方法。

当next 方法第一次被调用的时候,这个方法才开始执行,直到它遇到了一个yield语句。这个yield(量产)的值作为next的返回值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值