您可以使用Python生成器函数做什么? [英] What can you use Python generator functions for?

查看:88
本文介绍了您可以使用Python生成器函数做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Python,并且遇到了生成器函数,这些函数中包含yield语句.我想知道这些功能真正可以解决哪些类型的问题.

I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them. I want to know what types of problems that these functions are really good at solving.

推荐答案

生成器为您提供了懒惰的评估.您可以通过遍历它们来使用它们,可以显式地使用"for",也可以通过将其传递给任何迭代的函数或构造来隐式使用.您可以将生成器视为返回多个项目,就像它们返回一个列表一样,但是与其一次一次返回它们,而不是一次全部返回它们,而是将生成器功能暂停直到请求下一个项目.

Generators give you lazy evaluation. You use them by iterating over them, either explicitly with 'for' or implicitly by passing it to any function or construct that iterates. You can think of generators as returning multiple items, as if they return a list, but instead of returning them all at once they return them one-by-one, and the generator function is paused until the next item is requested.

生成器非常适合计算大量结果(特别是涉及循环本身的计算),在这些情况下,您不知道是否需要所有结果,或者您不想在以下位置为所有结果分配内存同一时间.或者对于生成器使用另一个生成器或消耗一些其他资源的情况,如果这种情况发生得越晚越方便.

Generators are good for calculating large sets of results (in particular calculations involving loops themselves) where you don't know if you are going to need all results, or where you don't want to allocate the memory for all results at the same time. Or for situations where the generator uses another generator, or consumes some other resource, and it's more convenient if that happened as late as possible.

生成器的另一个用途(实际上是相同的)是将迭代替换为迭代.在某些情况下,您希望函数执行大量工作,并偶尔向调用者报告.传统上,您将为此使用回调函数.您将此回调传递给工作函数,它将定期调用此回调.生成器方法是工作函数(现在是生成器)对回调一无所知,仅在需要报告某些内容时才产生.调用者没有编写单独的回调并将其传递给工作函数,而是在生成器周围的一个"for"循环中完成所有报告工作.

Another use for generators (that is really the same) is to replace callbacks with iteration. In some situations you want a function to do a lot of work and occasionally report back to the caller. Traditionally you'd use a callback function for this. You pass this callback to the work-function and it would periodically call this callback. The generator approach is that the work-function (now a generator) knows nothing about the callback, and merely yields whenever it wants to report something. The caller, instead of writing a separate callback and passing that to the work-function, does all the reporting work in a little 'for' loop around the generator.

例如,假设您编写了一个文件系统搜索"程序.您可以完整地执行搜索,收集结果,然后一次显示一个.在显示第一个结果之前,必须先收集所有结果,并且所有结果将同时存储在内存中.或者,您可以在找到结果时显示结果,这样可以提高内存效率,并且对用户友好得多.后者可以通过将结果打印功能传递给文件系统搜索功能来完成,也可以仅通过将搜索功能生成器并遍历结果来完成.

For example, say you wrote a 'filesystem search' program. You could perform the search in its entirety, collect the results and then display them one at a time. All of the results would have to be collected before you showed the first, and all of the results would be in memory at the same time. Or you could display the results while you find them, which would be more memory efficient and much friendlier towards the user. The latter could be done by passing the result-printing function to the filesystem-search function, or it could be done by just making the search function a generator and iterating over the result.

如果要查看后两种方法的示例,请参见os.path.walk()(带有回调的旧文件系统行走功能)和os.walk()(新的文件系统行走生成器).当然,如果您真的想将所有结果收集到列表中,则生成器方法可以轻松转换为大列表方法:

If you want to see an example of the latter two approaches, see os.path.walk() (the old filesystem-walking function with callback) and os.walk() (the new filesystem-walking generator.) Of course, if you really wanted to collect all results in a list, the generator approach is trivial to convert to the big-list approach:

big_list = list(the_generator)

这篇关于您可以使用Python生成器函数做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆