生成器可以调用吗?哪个是发电机? [英] Is a generator the callable? Which is the generator?

查看:72
本文介绍了生成器可以调用吗?哪个是发电机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成器只是一个函数,它返回一个可以在其后调用的对象,这样,对于每次调用,它都会返回一些值,直到引发StopIteration异常,表明已生成所有值.这样的对象称为迭代器.

A generator is simply a function which returns an object on which you can call next, such that for every call it returns some value, until it raises a StopIteration exception, signaling that all values have been generated. Such an object is called an iterator.

>>> def myGen(n):
...     yield n
...     yield n + 1
... 
>>> g = myGen(6)

我引用了了解Python中的生成器吗?

这就是我要弄清楚的:

  1. 发电机是谁? myGen myGen(6)?

根据上述引用,我认为生成器应为 myGen .而 myGen(6)是返回的迭代器对象.但是我真的不确定.

According to the quote mentioned above, I think the generator should be myGen. And myGen(6) is the returned iterator object. But I am really not sure about it.

当我尝试此操作时:

>>> type(myGen)
<type 'function'>
>>> type(g)         # <1>this is confusing me.
<type 'generator'>  
>>> callable(g)     # <2> g is not callable.  
False               
>>> callable(myGen)
True
>>> g is iter(g)    # <3> so g should an iterable and an iterator 
True                # at the same time. And it will be passed as an argument
>>> for i in g:     # to built-in function `next()` in a "for...in..." loop.
        print i     # (is that correct?)

6
7     

因此,根据< 1> < 2> g 的类型为'generator',而不是可召唤的.但是 生成器是可调用的,调用生成器会为您提供迭代器对象 这是怎么回事?

So, according to <1> and <2>, g's type is 'generator' and it is not callable. But generators are callable, and calling a generator gets you an iterator object What's going on here?

当我在寻找答案时,我碰到 每次您定义函数python都会创建一个可调用对象.

When I was searching for answers, I run into Every time you define a function python creates a callable object.

那么,我可以这样说吗?在定义函数 myGen 时, myGen 是指向可调用对象的名称,该对象是具有 __ call __ 方法.在这种情况下,当调用 myGen 时, myGen 是生成器,而 myGen(6)是返回的迭代器.

So, can I say something like this? when the function myGen is defined, myGen is a name referring to a callable object which is an instance of a class that has a __call__ method. In this case, myGen is a generator, and myGen(6) is the returned iterator when myGen is called.

但是为什么 type(g)完全返回< type'generator'> ?由于我在函数中没有 return 语句,因此返回的 iterator 东西在我看来也很可疑.

But why does type(g) return <type 'generator'> at all? And this returned iterator thing also looks suspicious to me since there is no return statement in the function.

不是 函数总是返回某些内容(至少是 None ,如果没有返回)-语句在执行过程中已到达,并且函数已到达结尾) ?

Isn't it that Functions always return something (at least None, when no return-statement was reached during execution and the end of the function is reached)?

推荐答案

不幸的是,该术语令人困惑,因为"generator"通常用于指代函数或返回的迭代器,因此很难说一种用法更多正确的.文档yield语句

The terminology is unfortunately confusing, as "generator" is so commonly used to refer to either the function or the returned iterator that it's hard to say one usage is more correct. The documentation of the yield statement says

yield语句仅在定义 generator函数时使用,并且仅在生成器函数的主体中使用.使用收益函数定义中的语句足以导致定义以创建一个生成器函数,而不是普通的功能.

The yield statement is only used when defining a generator function, and is only used in the body of the generator function. Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.

调用生成器函数时,它将返回称为a的迭代器. generator iterator ,或更常见的是 generator .

When a generator function is called, it returns an iterator known as a generator iterator, or more commonly, a generator.

介绍该概念的原始PEP

请注意,当从上下文中清除意图时,将使用非限定名称生成器"可用于指代生成器功能或生成器迭代器.

如果要明确区分,请在函数中使用"generator function",在迭代器中使用"generator iterator".

If you want to make a clear distinction, use "generator function" for the function and "generator iterator" for the iterator.

这篇关于生成器可以调用吗?哪个是发电机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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