, но второй вызов anext() приводит к странной ошибке?
Читая документацию anext(), я предполагаю, что предоставленное значение по умолчанию будет использоваться, когда нет доступного значения "next":
Если по умолчанию задано, оно возвращается, если итератор исчерпан, в противном случае вызывается StopAsyncIteration.
Выполнение этого в (интерактивном) интерпретаторе Python 3.12 на MacOS Apple M3 Pro,
Код: Выделить всё
import asyncio
async def generator(it=None):
if it is not None:
yield (it, it)
async def my_func():
# results in a=1 b=1
a, b = await anext(generator(1), (2, 3))
# results in no printing
async for a, b in generator():
print(a, b)
# raises exception
a, b = await anext(generator(), (2, 3))
loop = asyncio.new_event_loop()
loop.run_until_complete(my_func())
Код: Выделить всё
StopAsyncIteration
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "", line 1, in
File "/Users/manuel/.pyenv/versions/3.12.0/lib/python3.12/asyncio/base_events.py", line 664, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "", line 6, in my_func
SystemError: returned a result with an exception set
Подробнее здесь: https://stackoverflow.com/questions/792 ... lass-stopi
Мобильная версия