Python-3.2 coroutine: AttributeError: объект 'generator' не имеет атрибута 'next'
#!/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()
Это сопрограмма, но Python3.2 рассматривает ее как генератор - почему? Что здесь происходит? Я имею в виду Python Essential Reference Дэвида Беазели pg: 20.
Чтобы процитировать соответствующий раздел:
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
Почему мой код не работает - не работает БД.
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'
Ответы
Ответ 1
Вы отбрасываете сообщение об ошибке; по типу, Python не делает различий - вы можете .send
на все, что использует yield
, даже если оно ничего не делает с отправленным значением внутри.
В 3.x к ним больше не применяется .next
; вместо этого используйте встроенную свободную функцию next
.
Ответ 2
В случае, если вы обнаружите, что исправляете какой-то код, кажется, что встроенная функция python3 next() вызывает итератор next(), поэтому вы можете найти/заменить кого-то python2 .next(
с python3-tolerable .__next__(
, как я только что сделал, чтобы части модуля primff работают в python3 (среди других тривиальных изменений).
Здесь ссылка:
next (iterator [, default])
Извлеките следующий элемент из итератора, вызвав его next() метод. Если задано значение по умолчанию, оно возвращается, если итератор исчерпан, иначе StopIteration поднят.
Ответ 3
Для версии 3.2 python синтаксис встроенной функции next()
должен быть matcher.__next__()
или next(matcher)
.