yield

十一、yield生成器

六眼飞鱼酱① 提交于 2019-11-26 17:37:23
1、对比range 和 xrange 的区别 >>> print range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> print xrange(10) xrange(10) 如上代码所示,range会在内存中创建所有指定的数字,而xrange不会立即创建,只有在迭代循环时,才去创建每个数组。 ========================================================== def func1(): return 1 return 2 return 3 print (func1()) 问以上结果会打印什么?答案:只有1(因为当它遇到return时代表它的生命周期已经结束,就不会往下再走了)。 现在我把它改变一下,变成这样,问以上结果会打印什么? 答案:<generator object func1 at 0x02CF02D8> 告诉你它是一个生成器,只有你通过for循环才能逐个去调用它 def func1(): yield 1 yield 2 yield 3 print (func1()) for i in func1(): print (i) 返回 1 2 3 就像 for i in xrange(10) print (i) 返回 0 1 2 3 4 5 6 7 8 9 因为xrange也是一个生成器 ===

yield in list comprehensions and generator expressions

心已入冬 提交于 2019-11-26 17:22:21
The following behaviour seems rather counterintuitive to me (Python 3.4): >>> [(yield i) for i in range(3)] <generator object <listcomp> at 0x0245C148> >>> list([(yield i) for i in range(3)]) [0, 1, 2] >>> list((yield i) for i in range(3)) [0, None, 1, None, 2, None] The intermediate values of the last line are actually not always None , they are whatever we send into the generator, equivalent (I guess) to the following generator: def f(): for i in range(3): yield (yield i) It strikes me as funny that those three lines work at all. The Reference says that yield is only allowed in a function

What's the yield keyword in JavaScript?

♀尐吖头ヾ 提交于 2019-11-26 16:58:53
I heard about a "yield" keyword in JavaScript, but I found very poor documentation about it. Can someone explain me (or recommend a site that explains) its usage and what it is used for? The MDN documentation is pretty good, IMO. The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the

python——生成器函数

爱⌒轻易说出口 提交于 2019-11-26 16:55:56
1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样,取值方式和迭代器一样(__next__(),)。 send():的作用是给上一个yield传值 生成器一般由生成器函数或者生成器表达式来创建 其实就是手写的迭代器 生成器函数 和普通函数写法没有区别,里面有yield的函数就是生成器函数。 生成器函数在执行的时候,默认不会执行函数体,而是返回生成器, 通过生成器的__next__()分段执行这个函数 send():的作用是给上一个yield传值,不能在开头使用,因为没有上一个yield,最后一个yield也不可以用send,因为会报错停止生成器。 yield   yield:相当于return,可以返回数据。但是yield不会彻底中断函数。分段执行 函数中如果有yield,这个函数就是生成器函数。 生成器函数名() 获取的是生成器。这个时候不执行函数。 def func():   print('执行到第一yield,下面是返回值')   yield 1   # return和yield都可以返回数据   print('执行第一个yield不会打印这个条,再次使用gen.__next__()调用才会打印,下面是返回值')   yield 2 gen = func() # 拿到生成器,不会执行函数 gen.__next__() # 执行函数,执行到下一个yield gen._

How to use &#39;yield&#39; inside async function?

随声附和 提交于 2019-11-26 15:52:59
问题 I want to use generator yield and async functions. I read this topic, and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): mygenerator = await createGenerator() for i in mygenerator: print(i) loop = asyncio.get_event_loop() try: loop.run_until_complete(start()) except KeyboardInterrupt: loop.stop() pass But i got the error: SyntaxError: 'yield' inside async function How to use yield generator in

How Can I Wait In Node.js (Javascript), l need to pause for a period of time

本小妞迷上赌 提交于 2019-11-26 15:41:41
I'm developing a console like script for personal needs. I need to be able to pause for a extended amount of time, but, from my research, node.js has no way to stop as required. It’s getting hard to read users’ information after a period of time... I’ve seen some code out there, but I believe they have to have other code inside of them for them to work such as: setTimeout(function() { }, 3000); However, I need everything after this line of code to execute after the period of time. For example, //start-of-code console.log('Welcome to My Console,'); some-wait-code-here-for-ten-seconds..........

In practice, what are the main uses for the new “yield from” syntax in Python 3.3?

陌路散爱 提交于 2019-11-26 15:34:16
I'm having a hard time wrapping my brain around PEP 380 . What are the situations where "yield from" is useful? What is the classic use case? Why is it compared to micro-threads? [ update ] Now I understand the cause of my difficulties. I've used generators, but never really used coroutines (introduced by PEP-342 ). Despite some similarities, generators and coroutines are basically two different concepts. Understanding coroutines (not only generators) is the key to understanding the new syntax. IMHO coroutines are the most obscure Python feature , most books make it look useless and

Ruby&#39;s yield feature in relation to computer science

只谈情不闲聊 提交于 2019-11-26 15:16:39
问题 I recently discovered Ruby's blocks and yielding features, and I was wondering: where does this fit in terms of computer science theory? Is it a functional programming technique, or something more specific? 回答1: Ruby's yield is not an iterator like in C# and Python. yield itself is actually a really simple concept once you understand how blocks work in Ruby. Yes, blocks are a functional programming feature, even though Ruby is not properly a functional language. In fact, Ruby uses the method

WWW/UnityWebRequest POST/GET request won&#39;t return the latest data from server/url

試著忘記壹切 提交于 2019-11-26 14:47:40
问题 I am creating a HoloLens app using Unity which has to take data from a REST API and display it. I am currently using WWW datatype to get the data and yield return statement in a coroutine that will be called from the Update() function. When I try to run the code, I get the latest data from the API but when someone pushes any new data onto the API, it does not automatically get the latest data in real time and I have to restart the app to see the latest data. My Code: using UnityEngine; using

es6之Generator

放肆的年华 提交于 2019-11-26 14:45:13
1.Generator函数其实是一个封装了多个内部状态的状态机,执行它会返回一个遍历器对象,然后可以依次遍历Generator中的每一个状态,也就是分段执行,yield是暂停执行的标记,next恢复执行。 2.yield: - 一个函数里面,return只能执行一遍,yield可以执行多次; - Generator函数可以不用yield,这时就是一个简单的暂缓执行函数; - yield只能用在Generator函数里; - yield如果用在一个表达式里,必须放在圆括号里 function * foo() { console.log( 'hello' + (yield)); console.log( 'hello' + (yield 'world' )); } - yield用于函数参数或放在赋值表达式的右边,可以不加括号 function * demo() { foo(yield 'a', yield 'b' ); let input = yield; } 3.可以把Generator函数赋值给对象的Symbol.iterator属性,从而使对象具有Iterator接口 var obj = {}; obj[Symbol.iterator] = function * (){ yield 1 ; yield 2 ; }; [...obj] // [1,2] 4