C#的yield已经忘得差不多了。又遇到python的yield。
iterator
def testYield():
print 'yield1'
m = yield 1
print 'm =' , m
print 'yield2'
yield 5
for a in testYield():
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 <module>
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,生成器,会被区别对待。

举个栗子~

def testYield():

    m = yield 1
print type(m)
print m
m2 = yield 5
yield 88 result = testYield()
print result.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
<type 'str'>
fighting
5

File "C:\pytest\Sele\Generator.py", line 39, in <module>
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生产一个东西

再看一个栗子~

def h():
print 'Wen Chuan'
m = yield 5 # Fighting!
print 'm=', m
d = yield 12
print 'We are together!'
print d
test = yield 35
print test
c = h()
current = c.next() # start generator, stop at the first yield
print current
current = c.send('Fighting!') # send current yield 'fighting', stop at the next yield
print current
c.send('got it')
print list(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的返回值。

【Python学习】yield send我就说这么多的更多相关文章

  1. python学习 - yield

    def myYield2(): for i in range(3): yield '2222 i am in myYield2', 'i = ', i def myYield(): for i in ...

  2. 【Python学习之十】yield之send方法

    yield作用 简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator.下面以斐波拉 ...

  3. [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …

    [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source - [Pyth ...

  4. Python协程:从yield/send到async/await

    这个文章理好了脉落. http://python.jobbole.com/86069/ 我练 习了一番,感受好了很多... Python由于众所周知的GIL的原因,导致其线程无法发挥多核的并行计算能力 ...

  5. 【Python学习笔记之二】浅谈Python的yield用法

    在上篇[Python学习笔记之一]Python关键字及其总结中我提到了yield,本篇文章我将会重点说明yield的用法 在介绍yield前有必要先说明下Python中的迭代器(iterator)和生 ...

  6. 理解Python协程:从yield/send到yield from再到async/await

    Python中的协程大概经历了如下三个阶段:1. 最初的生成器变形yield/send2. 引入@asyncio.coroutine和yield from3. 在最近的Python3.5版本中引入as ...

  7. [Python学习笔记-005] 理解yield

    网络上介绍yield的文章很多,但大多讲得过于复杂或者追求全面以至于反而不好理解.本文用一个极简的例子给出参考资料[1]中的讲解,因为个人觉得其讲解最为通俗易懂,读者只需要对Python的列表有所了解 ...

  8. Python学习--22 异步I/O

    在同步IO中,线程启动一个IO操作然后就立即进入等待状态,直到IO操作完成后才醒来继续执行.而异步IO方式中,线程发送一个IO请求到内核,然后继续处理其他的事情,内核完成IO请求后,将会通知线程IO操 ...

  9. Python学习笔记(十)

    Python学习笔记(十): 装饰器的应用 列表生成式 生成器 迭代器 模块:time,random 1. 装饰器的应用-登陆练习 login_status = False # 定义登陆状态 def ...

随机推荐

  1. web服务-2、四种方法实现并发服务器-多线程,多进程,协程,(单进程-单线程-非堵塞)

    知识点:1.使用多线程,多进程,协程完成web并发服务器 2.单进程-单线程-非堵塞也可以实现并发服务器 1.多进程和协程的代码在下面注释掉的部分,我把三种写在一起了 import socket im ...

  2. [转] Java程序员学C#基本语法两个小时搞定(对比学习)

    Java程序员学C#基本语法两个小时搞定(对比学习)   对于学习一门新的语言,关键是学习新语言和以前掌握的语言的区别,但是也不要让以前语言的东西,固定了自己的思维模式,多看一下新的语言的编程思想. ...

  3. asp 获取url 返回值 和 对json 返回值的处理

    Function GetHttpPage(HttpUrl,endoce) If endoce = "" Then endoce = "GB2312" If Is ...

  4. Python金融大数据分析PDF

    Python金融大数据分析(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1CF2NhbgpMroLhW2sTm7IJQ 提取码:clmt 复制这段内容后打开百度网盘 ...

  5. C++ 控制台推箱子小游戏

              // 游戏菜单.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #in ...

  6. JavaScript的正则表达式的基础

    正则表达式:* 具体字符(字面值)*字符边界*字符集合[ace],[0123456789]*字符补集[^ qxz]: 不在qxz范围内*字符范围[a-z 0-9]*字符簇(系统定义好的常用集合)--- ...

  7. ECMA Script 6_模块加载方案 ES6 Module 模块语法_import_export

    1. 模块加载方案 commonJS 背景: 历史上,JavaScript 一直没有模块(module)体系, 无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来. 其他语言都有这项功能: ...

  8. Solve Error: MissingSchemaError: Schema hasn't been registered for model "YourModel".

    使用MongoDB的时候,如果遇到下面这个错误: /home/ec2-user/YourProject/node_modules/mongoose/lib/index.js: throw new mo ...

  9. vue菜鸟从业记:公司项目里如何进行前后端接口联调

    最近我的朋友王小闰进入一家新的公司,正好公司项目采用的是前后端分离架构,技术栈是王小闰非常熟悉的vue全家桶,后端用的是Java语言. 在前后端开发人员碰面之后,协商确定好了前端需要的数据接口(扯那么 ...

  10. 19.3.25 sql查询语句

    1.单表查询:select * from 表名 where id = 111 2.查询表内数据并以id排序:select * from 表名 order by id (降序:desc/升序:asc) ...