anext() 非同期イテレータの次の項目
最終更新:2026-09-22
anext() 非同期イテレータの次の項目
anext(awaitable) は awaitable の次の項目を返します;使い果たすと StopAsyncIteration を送出します。
| カテゴリ | 組み込み関数 |
|---|---|
| 種類 | 組み込み関数 |
| Python バージョン | 3.10+ |
📝 構文
anext(awaitable[, default])
⚙️ 引数
| iterator 必須 | An async iterator. |
|---|---|
| default 任意 | Value returned on exhaustion, when provided.(デフォルト値:None) |
戻り値:The next item of the async iterator, or default if exhausted.
💥 例外
StopAsyncIteration— Raised when exhausted with no default.TypeError— Raised when the argument is not an async iterator.
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
1
2
🏷️ 関連する組み込み関数
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qanext() と next() の違いは?
Aanext() は next() の非同期版で、非同期イテレータに使います。await anext(it) として呼ぶ必要があり、async 関数の中でのみ使えます。
Qanext(it) が尽きるとどうなりますか?
AStopAsyncIteration が発生します。default 引数を渡すと例外を投げずにその値を返し、next() と同じ仕組みです。
Q普通のイテレータを anext() に渡せますか?
A渡せません。TypeError になります。anext() は非同期イテレータ(__anext__ を実装したオブジェクト)のみを受け付けます。
Qanext() はどんな場面で使いますか?
A非同期ジェネレータ(async generator)や非同期イテレータを手動で消費するときです。例:await anext(it) で次の値を取得します。同期コードの next() に対応します。