Trouble understanding python generators

This question already has an answer here: What does the “yield” keyword do? 36 answers Generators (like all iterables) can only be iterated over once. By the time print_gen(x) is done, so is x . Any further attempts to get new values from x will result in StopIteration being raised. This works: x = do_gen() y = (incr_gen(i) for i in do_gen()) print_gen(x) print_gen(y) as that creates t

无法理解python生成器

这个问题在这里已经有了答案: “yield”关键字有什么作用? 36个答案 生成器(像所有迭代器)只能迭代一次。 到print_gen(x)完成时, x 。 任何进一步尝试从x获取新值都会导致StopIteration被提升。 这工作: x = do_gen() y = (incr_gen(i) for i in do_gen()) print_gen(x) print_gen(y) 因为这会创建两个独立的独立发电机 在您的版本中,您分配给y的生成器表达式期望x生成更多值。 当您对它们依次使用next()函数

How does a function in Python remember its values after it returns?

This question already has an answer here: What does the “yield” keyword do? 36 answers A simplified answer. If you have a function that generates a sequence of values, you may transform it in a generator by using yield. def func(): for i in range(3): yield i list(func()) ==> [0,1,2] for el in func(): print(el) # 0 # 1 # 2 Each time yield i

Python中的函数在返回后如何记住它的值?

这个问题在这里已经有了答案: “yield”关键字有什么作用? 36个答案 一个简单的答案。 如果你有一个函数可以产生一系列值,你可以使用yield来将它转换成一个generator 。 def func(): for i in range(3): yield i list(func()) ==> [0,1,2] for el in func(): print(el) # 0 # 1 # 2 每次调用yield时,函数都会被冻结。 当它再次被调用时,它会从最后一个状态继续,

What does yield do in python 2.7?

Possible Duplicate: The Python yield keyword explained Okay, I've probably phrased the question badly but this is the situation I have. I have this line of code in Python 2.7 which I'm trying to understand: yield (padding_zeros + number_string).encode("ascii") In this line of code, padding_zeros is a string of a variable number of '0's and number_string is a number in the

python 2.7有什么收益?

可能重复: 解释了Python yield关键字 好吧,我可能很糟糕地表述了这个问题,但这是我的情况。 我在Python 2.7中有这行代码,我试图理解: yield (padding_zeros + number_string).encode("ascii") 在这行代码中, padding_zeros是一个可变数字'0'的字符串, number_string是一个字符串形式的数字,可以是0到1之间的任何数字,比如10000。 我非常有信心.encode("ascii")只是将yield的输出转换为asci

What is a "yield" statement in a function?

Possible Duplicate: The Python yield keyword explained Can someone explain to me what the yield statement actually does in this bit of code here: def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a+b for number in fibonacci() : # Use the generator as an iterator; print number What I understand so far is, we are defining a function finonacci() , with

什么是函数中的“yield”声明?

可能重复: 解释了Python yield关键字 有人可以向我解释这段代码中的yield语句实际做了什么: def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a+b 对于fibonacci() :#使用生成器作为迭代器; 打印号码 到目前为止我所了解的是,我们正在定义一个函数finonacci() ,但没有参数? 在函数内部,我们定义a和b等于0和1,接下来,虽然这是真的,但我们正在产生a 。 这究竟是在

for x in y(): how does this work?

This question already has an answer here: What does the “yield” keyword do? 36 answers Using yield turns a function into a generator. A generator is a specialized type of iterator. for always loops over iterables, taking each element in turn and assigning it to the name(s) you listed. spinning_cursor() returns a generator, the code inside spinning_cursor() doesn't actually run until

for x in y():这是如何工作的?

这个问题在这里已经有了答案: “yield”关键字有什么作用? 36个答案 使用yield将函数转换为一个生成器。 生成器是一种特殊类型的迭代器。 for总是循环遍历iterables,采取每个元素依次并将其分配给您列出的姓名(或名称)。 spinning_cursor()返回一个生成器,在你开始迭代生成器之前, spinning_cursor()中的代码实际上并不运行。 遍历生成器意味着函数中的代码将被执行,直至遇到yield语句,此时表达式的结果将作为

How to inspect program state in the presence of generators/coroutines?

With normal functions calls, the program state is mostly described by a simple call stack. It is printed out as a traceback after an uncaught exception, it can be examined with inspect.stack , and it can be displayed in a debugger after a breakpoint. In the presence of generators, generator-based couroutines, and async def -based coroutines, I don't think the call stack is enough. What

如何检查程序状态是否存在发生器/协程?

对于正常的函数调用,程序状态主要由一个简单的调用堆栈来描述。 它在未捕获的异常之后作为回溯被打印出来,它可以用inspect.stack检查,并且可以在断点后显示在调试器中。 在发生器,基于生成器的例程和async def基于async def的协程的情况下,我认为调用堆栈不够。 什么是精神上可视化程序状态的好方法? 我如何在运行时检查它? 有函数inspect.getgeneratorstate和inspect.getcoroutinestate ,但它们仅提供有关生成器

Python vs Cpython

What's all this fuss about Python and CPython (Jython,IronPython), I don't get it: python.org mentions that CPython is: The "traditional" implementation of Python (nicknamed CPython) yet another Stack Overflow question mentions that: CPython is the default byte-code interpreter of Python, which is written in C. Honestly I don't get what both of those explanations p

Python与Cpython

关于Python和CPython(Jython,IronPython)的大惊小怪,我不明白: python.org提到CPython是: Python的“传统”实现(绰号CPython) 另一个Stack Overflow问题提到: CPython是Python的默认字节码解释器,用C语言编写。 老实说,我没有得到这些解释实际上意味着什么,但我认为是什么,如果我使用CPython,这意味着当我运行一个示例python代码时,它将它编译为C语言,然后执行它,就好像它是C码 那么CPython究竟是什

How do greenlets work?

How are greenlets implemented? Python uses the C stack for the interpreter and it heap-allocates Python stack frames, but beyond that, how does it allocate/swap stacks, how does it hook into the interpreter and function call mechanisms, and how does this interact with C extensions? (Any quirks)? There are some comments at the top of greenlet.c in the source, but they're a bit opaque. FWI

greenlet如何工作?

greenlet如何实施? Python使用C栈作为解释器,它堆 - 分配Python堆栈帧,但除此之外,它如何分配/交换堆栈,它如何挂接到解释器和函数调用机制,以及它如何与C扩展交互? (任何怪癖)? 源代码中greenlet.c的顶部有一些注释,但它们有点不透明。 FWIW我来自不熟悉CPython内部但熟悉底层系统编程,C,线程,事件,协程/合作线程,内核编程等的人。 (一些数据点:它们不使用ucontext.h,它们在每个上下文切换上执行2次me

How do you return multiple values in Python?

The canonical way to return multiple values in languages that support it is often tupling. Option: Using a tuple Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0,y1,y2) However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you could keep tupling them, but it ge

你如何在Python中返回多个值?

在支持它的语言中返回多个值的规范方式通常很糟糕。 选项:使用元组 考虑这个微不足道的例子: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0,y1,y2) 但是,随着返回值的数量增加,这很快就会出现问题。 如果你想返回四个或五个值呢? 当然,你可以继续把它们弄脏,但很容易忘记哪个值是在哪里。 将它们解包到任何你想接收它们的地方也是相当难看的。 选项:使用字典 下一个合乎逻辑的步骤似

What limitations have closures in Python compared to language X closures?

Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which supports some flavour of closures. Some limitations are mentioned in the Closures in Python (compared to Ruby's closures), but the article is old and many limitations do not exist in modern Python any more. Seeing a code example for a concrete limitation would be great. Related questions : Can yo

与X语言关闭相比,Python有什么限制?

其中X是支持某些闭包的编程语言(C#,Javascript,Lisp,Perl,Ruby,Scheme等)。 Python中的闭包(与Ruby的闭包相比)中提到了一些限制,但是这篇文章比较陈旧,并且现代Python中不再存在很多限制。 看到具体限制的代码示例会很好。 相关问题 : 你能解释关闭(因为它们与Python有关)吗? 什么是'关闭'? javascript关闭如何工作? 目前最重要的限制是您无法分配给外部变量。 换句话说,闭包是只读的