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
iterator
is exhausted anddefault
is not set
If
default
is given, it is returned if theiterator
is exhausted. Otherwise,StopAsyncIteration
is raised for an exhaustediterator
.Note
This function is not async neutral. The
iterator
must be an asynchronous iterator, i.e. support the__anext__()
method.
- await all(iterable: (async) iter T) bool [source]¶
Return
True
if none of the elements of the (async)iterable
are false
- await any(iterable: (async) iter T) bool [source]¶
Return
False
if none of the elements of the (async)iterable
are 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
iterable
is empty anddefault
is not set
The
key
argument 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. Thedefault
argument specifies an object to return if the providediterable
is empty. If theiterable
is empty anddefault
is not provided, aValueError
is 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
iterable
is empty anddefault
is not set
The
key
argument 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. Thedefault
argument specifies an object to return if the providediterable
is empty. If theiterable
is empty anddefault
is not provided, aValueError
is 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
subject
does not support any iteration protocol
If
sentinel
is not given, thesubject
must 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
sentinel
is 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,
filter
is equivalent to(element async for args in iterable if await func(element))
.The
function
may be a regular or async callable. Theiterable
may 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
iterables
are not equal length andstrict
is set
The next element of
zip
is atuple
of the next element of each of itsiterables
. As soon as any of itsiterables
is exhausted,zip
is exhausted as well. This means that ifzip
receives 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
iterables
is empty, thezip
iterator is empty as well. Multipleiterables
may be mixed regular and async iterables.When called with
strict=True
, alliterables
must be of same length; in this modezip
raisesValueError
if anyiterables
are not exhausted with the others.Added in version 3.10.0: The
strict
parameter.
- async for :R in map(function: (T, ...) → (await) R, iterable: (async) iter T, ...)[source]¶
An async iterator mapping an (async) function to items from (async) iterables
At each step,
map
collects the next item from each iterable and callsfunction
with all items; iffunction
provides an awaitable, it isawait
ed. The result is the next value ofmap
. Barring sync/async translation,map
is equivalent to(await function(*args) async for args in zip(iterables))
.It is important that
func
receives one item from each iterable at every step. For niterable
,func
must take n positional arguments. Similar tozip()
,map
is exhausted as soon as its first argument is exhausted.The
function
may be a regular or async callable. Multipleiterable
may be mixed regular and async iterables.
- 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
start
for the first element ofiterable
, and is incremented by1
for each further element. Theiterable
may be a regular or async iterable.
Standard types¶
- await dict(iterable: (async) iter (str, T) = ()) {str: T, ...} [source]¶
Create a
dict
from 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
list
from an (async) iterableThis is equivalent to
[element async for element in iterable]
.
- await set(iterable: (async) iter T = ()) {T, ...} [source]¶
Create a
set
from an (async) iterableThis is equivalent to
{element async for element in iterable}
.
- await tuple(iterable: (async) iter T = ()) -> (T, ...)[source]¶
Create a
tuple
from 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
key
argument specifies a one-argument (async) callable, which provides a substitute for determining the sort order of each item. The special value and defaultNone
represents the identity functions, i.e. compares items directly.The default sort order is ascending, that is items with
a < b
implyresult.index(a) < result.index(b)
. Usereverse=True
for descending sort order.Note
The actual sorting is synchronous, so a very large
iterable
or 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.