yield

python第十三课

不羁的心 提交于 2019-11-26 23:55:15
#函数 --2天 #函数的定义和调用 #def 函数名(形参) #函数体 #return返回值 #调用 函数名(实参) #站在形参的角度上:位置参数,*args,默认参数(陷阱),**kwargs #站在实参的角度上:按位置传,按照关键字传 #返回值:没有返回值 返回一个值 返回多个值 #接收返回值:没有返回值不接收,返回一个值作一个变量接收,返回多个值用一个变量或者对应数目变量接收#闭包函数 --在内部函数引用外部函数的变量#装饰器函数 --装饰器一定是闭包函数 #装饰器的作用 --在不改变原来函数的调用方式的情况下,在这个函数前后添加新的功能 #完美的符合了一个开发原则:开放封闭原则 #对扩展是开发的 #对修改是封闭的 #基础的装饰器 # from functools import wraps # def wrapper(func): # @wraps(func) # def inner(*args,**kwargs): # '''在函数被调用之前添加的代码''' # ret=func(*args,**kwargs) #func是被装饰的函数,在这里被调用 # return ret # return inner # 使用---@wrapper # def func(): #inner # pass #func name # 带参数的装饰器 def outer(形参): def

迭代器与生成器

梦想与她 提交于 2019-11-26 23:25:29
一、迭代器 1、什么是迭代器协议 1、迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个Stopiteration异常,以终止迭代。(只能往后走不能往前退) 2、可迭代对象:实现了迭代器协议的对象。(如何实现:对象内部定义了一个__iter__()方法) 3、协议是一种约定,可迭代对象实现了迭代器协议,python的内部工具(如for循环,sum,min,max函数等)使用迭代器协议访问对象。 2、迭代器与可迭代对象 1、迭代器:可以被 next() 函数调用并不断返回下一个值的对象称为迭代器。 2、可迭代对象:可以直接作用于 for 循环的对象。 3、for循环 for循环的本质:循环所有的对象,全部都是使用迭代器协议。 for循环的实现: L = [1,2,3,4,5] L = L.__iter__() #使用内部方法__iter__()将其变为可迭代对象 print(L.__next__()) #使用可迭代对象提供的方法__next__()循环取值 print(L.__next__()) print(L.__next__()) print(L.__next__()) print(L.__next__()) View Code 可迭代对象会抛出 StopIteration ,为了解决这一问题,加入异常处理,模拟真实的for循环机制 L

How does this function with a “yield” work in detail?

别说谁变了你拦得住时间么 提交于 2019-11-26 22:58:13
问题 I got this method (inside a Unity C# Script), but I do not understand how the "yield" part actually works. I know from the MSDN that the function will return an IEnumerator which I could iterate throught, but this code waits 1,5 seconds and does not get iterated because this would mean, the objects created inside were created multiple times. Anyone here who can explain me how this code works? IEnumerator DestroyShip() { // create new gameobject Instantiate(ExplosionPrefab, transform.position,

Does Scala have an equivalent to C# yield?

穿精又带淫゛_ 提交于 2019-11-26 22:58:07
问题 I'm new to Scala, and from what I understand yield in Scala is not like yield in C#, it is more like select. Does Scala have something similar to C#'s yield? C#'s yield is great because it makes writing iterators very easy. Update: here's a pseudo code example from C# I'd like to be able to implement in Scala: public class Graph<T> { public IEnumerable<T> BreadthFirstIterator() { List<T> currentLevel = new List<T>(); currentLevel.add(_root); while ( currentLevel.count > 0 ) { List<T>

Python中yield的用法详解

女生的网名这么多〃 提交于 2019-11-26 20:03:49
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真TM的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易懂的,我就想问没有有考虑过读者的感受。 接下来是正题: 首先,如果你还没有对yield有个初步分认识,那么你先把yield看做“return”,这个是直观的,它首先是个return,普通的return是什么意思,就是在程序中返回某个值,返回之后程序就不再往下运行了。看做return之后再把它看做一个是生成器(generator)的一部分(带yield的函数才是真正的迭代器),好了,如果你对这些不明白的话,那先把yield看做return,然后直接看下面的程序,你就会明白yield的全部意思了: def foo(): print("starting...") while True: res = yield 4 print("res:",res)g = foo()print(next(g))print("*"*20)print(next(g)) 就这么简单的几行代码就让你明白什么是yield,代码的输出这个: starting...4********************res: None4 我直接解释代码运行顺序,相当于代码单步调试: 1.程序开始执行以后

python生成器与表达式

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 19:58:30
生成器函数:使用常规的def语句进行编写,但是使用 yield语句一次返回一个结果,在每次结果产生之间挂起和恢复他们的状态。 生成器表达式:类似列表推导,但是,它们返回按需产生结果的一个对象,而不是创建一个结果列表。 def gensquares(N): for i in range(N): yield i**2 for i in gensquares(5): print(i,end=':') #0:1:4:9:16: x=gensquares(r) next(x) #0 next(x) #1 next(x) #4 返回的生成器对象有一个__next__方法。 生成器对大型程序而言,在内存使用和性能方面都更好。它们允许函数避免预先做好所有的工作,在结果的列表很大或者在处理每一个结果都需要很多时间时,生成器将产生一系列值的时间分散到每一次的循环迭代中去。 生成器的send方法生成一系列结果的下一个元素,值可以通过调用G.send(value)发送给一个生成器G。之后恢复生成器代码的执行,并且生成器中的yield表达式返回了发送给send函数的值。如果提前调用了正常的G.__next__方法,yield则返回None。 def gen(): for i in range(10): x=yield i print(x) G=gen() next(G) #0 G.send(77) #77

Is there a Java equivalent to C#'s 'yield' keyword?

£可爱£侵袭症+ 提交于 2019-11-26 19:40:28
I know there is no direct equivalent in Java itself, but perhaps a third party? It is really convenient. Currently I'd like to implement an iterator that yields all nodes in a tree, which is about five lines of code with yield. The two options I know of is Aviad Ben Dov's infomancers-collections library from 2007 and Jim Blackler's YieldAdapter library from 2008 (which is also mentioned in the other answer). Both will allow you to write code with yield return -like construct in Java, so both will satisfy your request. The notable differences between the two are: Mechanics Aviad's library is

yield statement implementation

本小妞迷上赌 提交于 2019-11-26 18:59:51
I want to know everything about the yield statement, in an easy to understand form. I have read about the yield statement and its ease when implementing the iterator pattern. However, most of it is very dry. I would like to get under the covers and see how Microsoft handles return yield. Also, when do you use yield break? yield works by building a state machine internally. It stores the current state of the routine when it exits and resumes from that state next time. You can use Reflector to see how it's implemented by the compiler. yield break is used when you want to stop returning results.

In C#, why can't an anonymous method contain a yield statement?

五迷三道 提交于 2019-11-26 17:57:21
问题 I thought it would be nice to do something like this (with the lambda doing a yield return): public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { IList<T> list = GetList<T>(); var fun = expression.Compile(); var items = () => { foreach (var item in list) if (fun.Invoke(item)) yield return item; // This is not allowed by C# } return items.ToList(); } However, I found out that I can't use yield in anonymous method. I'm wondering why. The yield docs just say it

implementing a state machine using the “yield” keyword

泪湿孤枕 提交于 2019-11-26 17:38:37
问题 Is it feasible to use the yield keyword to implement a simple state machine as shown here. To me it looks like the C# compiler has done the hard work for you as it internally implements a state machine to make the yield statement work. Can you piggy-back on top of the work the compiler is already doing and get it to implement most of the state machine for you? Has anyone done this, is it technically possible? 回答1: It's feasible but it is a bad idea. Iterator blocks were created to help you