The builtins library¶
The asyncstdlib.builtins library implements
Python’s Built-in Functions for (async) functions and (async) iterables.
Iterator reducing¶
- await anext(iterable: async iter T[, default: T]) T[source]¶
Retrieve the next item from the async iterator
- Raises:
StopAsyncIteration – if
iteratoris exhausted anddefaultis not set
If
defaultis given, it is returned if theiteratoris exhausted. Otherwise,StopAsyncIterationis raised for an exhaustediterator.Note
This function is not async neutral. The
iteratormust be an asynchronous iterator, i.e. support the__anext__()method.
- await all(iterable: (async) iter T) bool[source]¶
Return
Trueif none of the elements of the (async)iterableare false
- await any(iterable: (async) iter T) bool[source]¶
Return
Falseif none of the elements of the (async)iterableare true
- await max(iterable: (async) iter T, *, key: (T) → Any, default: T) T[source]¶
Return the largest item from an (async) iterable or from two or more values
- Raises:
ValueError – if
iterableis empty anddefaultis not set
The
keyargument specifies a one-argument ordering function like that used forlist.sort(). It may be a regular or async callable and defaults to the identity function. Thedefaultargument specifies an object to return if the providediterableis empty. If theiterableis empty anddefaultis not provided, aValueErroris raised.Note
The two-or-more-arguments variant is not supported, as it does not benefit from being
async. Use the builtinmax()function instead.
- await min(iterable: (async) iter T, *, key: (T) → Any, default: T) T[source]¶
Return the smallest item from an (async) iterable or from two or more values
- Raises:
ValueError – if
iterableis empty anddefaultis not set
The
keyargument specifies a one-argument ordering function like that used forlist.sort(). It may be a regular or async callable and defaults to the identity function. Thedefaultargument specifies an object to return if the providediterableis empty. If theiterableis empty anddefaultis not provided, aValueErroris raised.Note
The two-or-more-arguments variant is not supported, as it does not benefit from being
async. Use the builtinmin()function instead.
Iterator transforming¶
- async for :T in iter(iterable: (async) iter T)[source]¶
An async iterator object yielding elements from
subject- Raises:
TypeError – if
subjectdoes not support any iteration protocol
If
sentinelis not given, thesubjectmust support the async iteration protocol (the__aiter__()method), the regular iteration protocol (the__iter__()method), or it must support the sequence protocol (the__getitem__()method with integer arguments starting at 0). In either case, an async iterator is returned.If
sentinelis given, subject must be an (async) callable. In this case,iter()provides an async iterator that usesawait subject()to produce new values. Once a value equalssentinel, the value is discarded and iteration stops.See also
Use
scoped_iter()to ensure an (async) iterable is eventually closed and only borrowed until then.
- async for :T in filter(function: (T) → (await) bool, iterable: (async) iter T)[source]¶
An async iterator of elements in an (async) iterable filtered by an (async) callable
Barring sync/async translation,
filteris equivalent to(element async for args in iterable if await func(element)).The
functionmay be a regular or async callable. Theiterablemay be a regular or async iterable.
- async for :(T, ...) in zip(*iterables: (async) iter T, strict: bool = True)[source]¶
Create an async iterator that aggregates elements from each of the (async) iterables
- Raises:
ValueError – if the
iterablesare not equal length andstrictis set
The next element of
zipis atupleof the next element of each of itsiterables. As soon as any of itsiterablesis exhausted,zipis exhausted as well. This means that ifzipreceives niterables, with the shortest having m elements, it becomes a generator m-times producing an n-tuple.async for va, vb, vc in zip(a, b, c): print(f'a => {va}, b => {vb}, c => {vc}'
If
iterablesis empty, thezipiterator is empty as well. Multipleiterablesmay be mixed regular and async iterables.When called with
strict=True, alliterablesmust be of same length; in this modezipraisesValueErrorif anyiterablesare not exhausted with the others.Added in version 3.10.0: The
strictparameter.
- async for :R in map(function: (T, ...) → (await) R, iterable: (async) iter T, ..., /, strict: bool = True)[source]¶
An async iterator mapping an (async) function to items from (async) iterables
- Raises:
ValueError – if the
iterablesare not equal length andstrictis set
At each step,
mapcollects the next item from each iterable and callsfunctionwith these items; iffunctionprovides an awaitable, it isawaited. The result is the next value ofmap. Barring sync/async translation,mapis equivalent to(await function(*args) async for args in zip(iterables)).It is important that
funcreceives one item from each iterable at every step. For niterables,funcmust take n positional arguments. Similar tozip(),mapis exhausted as soon as any of its iterables is exhausted. When called withstrict=True, alliterablesmust be of same length; in this modemapraisesValueErrorif anyiterablesare not exhausted with the others.The
functionmay be a regular or async callable. Multipleiterablesmay be mixed regular and async iterables.Added in version 3.14.0: The
strictparameter.
- async for :(int, T) in enumerate(iterable: (async) iter T, start=0)[source]¶
An async iterator of running count and element in an (async) iterable
The count begins at
startfor the first element ofiterable, and is incremented by1for each further element. Theiterablemay be a regular or async iterable.
Standard types¶
- await dict(iterable: (async) iter (str, T)=()) {str: T, ...}[source]¶
Create a
dictfrom an (async) iterable and keywordsThis is equivalent to
{key: value async for key, value in iterable}if no keywords are provided.
- await list(iterable: (async) iter T = ()) [T, ...][source]¶
Create a
listfrom an (async) iterableThis is equivalent to
[element async for element in iterable].
- await set(iterable: (async) iter T = ()) {T, ...}[source]¶
Create a
setfrom an (async) iterableThis is equivalent to
{element async for element in iterable}.
- await tuple(iterable: (async) iter T = ()) -> (T, ...)[source]¶
Create a
tuplefrom an (async) iterable
- await sorted(iterable: (async) iter T, *, key: (T) → (await) Any, reverse: bool) [T, ...][source]¶
Sort items from an (async) iterable into a new list
The optional
keyargument specifies a one-argument (async) callable, which provides a substitute for determining the sort order of each item. The special value and defaultNonerepresents the identity functions, i.e. compares items directly.The default sort order is ascending, that is items with
a < bimplyresult.index(a) < result.index(b). Usereverse=Truefor descending sort order.Note
The actual sorting is synchronous, so a very large
iterableor very slow comparison may block the event loop notably. It is guaranteed to be worst-case O(n log n) runtime.Added in version 3.9.0.