Python-3.2 协程:AttributeError:'generator' 对象没有属性 'next'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21622193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:16:50  来源:igfitidea点击:

Python-3.2 coroutine: AttributeError: 'generator' object has no attribute 'next'

pythonpython-3.x

提问by

#!/usr/bin/python3.2
import sys

def match_text(pattern):
    line = (yield)
    if pattern in line:
        print(line)

x = match_text('apple')
x.next()

for line in input('>>>> '):
    if x.send(line):
        print(line)

x.close()

This is a coroutine but Python3.2 sees it as a generator - why? What is going on here? I'm referring to Python Essential Reference by David Beazeley pg:20.

这是一个协程,但 Python3.2 将其视为生成器 - 为什么?这里发生了什么?我指的是 David Beazeley pg:20 的 Python Essential Reference。

To quote the relevant section:

引用相关部分:

Normally, functions operate on a single set of input arguments. However, a function can
also be written to operate as a task that processes a sequence of inputs sent to
it.This type of function is known as a coroutine and is created by using the yield 
statement as an expression (yield) as shown in this example:
 def print_matches(matchtext):
   print "Looking for", matchtext
   while True:
     line = (yield)       # Get a line of text
     if matchtext in line:
       print line

To use this function, you first call it, advance it to the first (yield), and then 
start sending data to it using send(). For example:
>>> matcher = print_matches("python")
>>> matcher.next() # Advance to the first (yield)
Looking for python
>>> matcher.send("Hello World")
>>> matcher.send("python is cool")
python is cool
>>> matcher.send("yow!")
>>> matcher.close() # Done with the matcher function call

Why doesn't my code work - not that DB's works..

为什么我的代码不起作用 - 不是 DB 的作品..

deathstar> python3.2 xxx   
Traceback (most recent call last):
  File "xxx", line 9, in <module>
    matcher.next() # Advance to the first (yield)
AttributeError: 'generator' object has no attribute 'next'

采纳答案by Karl Knechtel

You're getting thrown off by the error message; type-wise, Python doesn't make a distinction - you can .sendto anything that uses yield, even if it doesn't do anything with the sent value internally.

你被错误信息抛弃了;在类型方面,Python 没有区别 - 您可以.send使用任何使用yield,即使它在内部不对发送的值执行任何操作。

In 3.x, there is no longer a .nextmethod attached to these; instead, use the built-in free function next:

在 3.x 中,不再有.next附加到这些的方法;相反,使用内置的 free 函数next

next(matcher)

回答by sage

In the case you find yourself patching somebody's code, it seems that the built-in python3 next() function calls the iterator's next() function, so you may be able to find/replace somebody's python2 .next(with the python3-tolerable .__next__(as I just did to make portions of the primefac module work in python3 (among other trivial changes).

如果您发现自己正在修补某人的代码,似乎内置的 python3 next() 函数调用了迭代器的next() 函数,因此您可以像我刚才那样.next(使用 python3-tolerable查找/替换某人的 python2.__next__(使 primefac 模块的一部分在 python3 中工作(以及其他微不足道的更改)。

Here's the reference:

这是参考

next(iterator[, default])

Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

下一个(迭代器 [,默认])

通过调用其next() 方法从迭代器中检索下一项。如果给出默认值,则在迭代器耗尽时返回,否则引发 StopIteration。

回答by Kalaiselvi Rajasekar R

For python version 3.2 the syntax for the next()in-built function should be matcher.__next__()or next(matcher).

对于 python 3.2 版next(),内置函数的语法应该是matcher.__next__()or next(matcher)