anext() 非同期の次の要素
最終更新:2026-09-22
anext() 非同期の次の要素
anext(async_iterator[, default]) は非同期イテレータから次の要素を返します。
| カテゴリ | 組み込み関数 |
|---|---|
| 種類 | 組み込み関数 |
| Python バージョン | 3.10+ |
📝 構文
anext(async_iterator[, default])
⚙️ 引数
| async_iterator 必須 | An async iterator. |
|---|---|
| default 任意 | Value returned when the async iterator is exhausted and no fallback is given. |
戻り値:The next item from the async iterator; returns default if exhausted and default is given.
💥 例外
StopAsyncIteration— Raised when the async iterator is exhausted and no default is given.
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
1
2
end
🏷️ 関連する組み込み関数
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qanext(it) と next(it) の違いは?
Anext() は同期的に次の要素を返します。anext() は非同期イテレータ用で、__anext__() はコルーチンを返すため await が必須です(await anext(it))。対応関係は iter↔next、aiter↔anext です。
Q非同期イテレータが尽きたらどうなりますか?
Adefault を指定しないと StopAsyncIteration を送出します。第二引数 default(例:anext(it, 0))を渡すと、尽きたときに例外を出さず default を返します。
Qanext は async 環境でのみ使えますか?
A基本的にはそうです。__anext__() はコルーチンを返すため、通常は async 関数内で await します。async for は内部で自動的に呼び出します。関数自体はどこでも呼べますが、結果を得るには await が必要です。
Qdefault を使わず安全に要素を取るには?
Aawait anext(it) を try/except StopAsyncIteration で包みます。もっと簡単なのは直接 async for で回すことで、内部で正常に終了を処理します。