简体   繁体   English

发电机是否可以调用? 哪个是发电机?

[英]Is a generator the callable? Which is the generator?

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. 生成器只是一个函数,它返回一个可以在其上调用的对象,这样每次调用它都会返回一些值,直到它引发一个StopIteration异常,表示已生成所有值。 Such an object is called an iterator. 这样的对象称为迭代器。

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

I quoted this from Understanding Generators in Python? 我从Python中理解生成器中引用了这个

Here is what I am trying to figure out: 这是我想弄清楚的:

  1. Which is the generator? 哪个是发电机? myGen or myGen(6) ? myGenmyGen(6)

    According to the quote mentioned above, I think the generator should be myGen . 根据上面提到的报价,我认为发电机应该是myGen And myGen(6) is the returned iterator object. myGen(6)是返回的迭代器对象。 But I am really not sure about it. 但我真的不确定。

  2. When I tried this: 当我尝试这个时:

     >>> 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 

So, according to <1> and <2> , g 's type is 'generator' and it is not callable. 因此,根据<1><2>g的类型是'generator',它不可调用。 But generators are callable, and calling a generator gets you an iterator object What's going on here? 但是生成器是可调用的,调用生成器会获得一个迭代器对象这里发生了什么?

When I was searching for answers, I run into Every time you define a function python creates a callable object. 当我在寻找答案时,我会遇到每次定义函数时python都会创建一个可调用对象。

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 . 当定义函数myGen时, myGen是一个引用可调用对象的名称,该对象是具有__call__方法的类的实例 In this case, myGen is a generator, and myGen(6) is the returned iterator when myGen is called. 在这种情况下, myGen是一个生成器, myGen(6)是调用myGen时返回的迭代器。

But why does type(g) return <type 'generator'> at all? 但是为什么type(g)返回<type 'generator'>呢? And this returned iterator thing also looks suspicious to me since there is no return statement in the function. 这个返回的iterator对我来说也很可疑,因为函数中没有return语句。

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) ? 是不是函数总是返回一些东西(至少是None ,当在执行期间没有达到return语句并且到达函数结束时)

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 屈服声明文件

The yield statement is only used when defining a generator function , and is only used in the body of the generator function. yield语句仅在定义生成器函数时使用 ,并且仅在生成器函数的主体中使用。 Using a yield statement in a function definition is sufficient to cause that definition to create a generator function instead of a normal function. 在函数定义中使用yield语句足以使该定义创建生成器函数而不是正常函数。

When a generator function is called, it returns an iterator known as a generator iterator , or more commonly, a generator . 当调用生成器函数时,它返回一个称为生成器迭代器的迭代器 ,或者更常见的是生成器

The original PEP introducing the concept says 最初的PEP介绍了这个概念

Note that when the intent is clear from context, the unqualified name "generator" may be used to refer either to a generator-function or a generator-iterator . 注意,当从上下文中清楚意图时,非限定名称“generator”可用于指代生成器函数或生成器迭代器

If you want to make a clear distinction, use "generator function" for the function and "generator iterator" for the iterator. 如果要明确区分,请对函数使用“generator function”,对迭代器使用“generator iterator”。

1) myGen is function which when invoked returns a generator object - so yes, myGen(6) is a generator object. 1) myGen是一个函数,当被调用时返回一个生成器对象 - 所以是的, myGen(6)是一个生成器对象。

2) Generators supply __iter__ and next() : docs 2)生成器提供__iter__next()docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM