11.. XXX document all delegations to __special__ methods
22 .. _built-in-funcs :
33
4- Built-in Functions
4+ Built-in functions
55==================
66
77The Python interpreter has a number of functions and types built into it that
@@ -65,14 +65,54 @@ are always available. They are listed here in alphabetical order.
6565
6666
6767.. function :: aiter(async_iterable, /)
68+ aiter(callable, /, stop_value, *, stop_exception=StopAsyncIteration)
69+ aiter(callable, /, *, stop_exception)
70+
71+ Return an :term: `asynchronous iterator ` object.
72+ The first argument is interpreted very differently
73+ depending on the presence of the other arguments.
74+ Without other arguments,
75+ the single argument must be an :term: `asynchronous iterable `,
76+ and the result is equivalent to calling ``x.__aiter__() ``.
77+
78+ If *stop_value * or *stop_exception * is given,
79+ then the first argument must be a callable object.
80+ The asynchronous iterator created in this case
81+ calls *callable * with no arguments and awaits the result
82+ for each call to its :meth: `~object.__anext__ ` method;
83+ if the awaited value is equal to *stop_value *,
84+ or if the call raises an exception matching *stop_exception *,
85+ :exc: `StopAsyncIteration ` will be raised,
86+ otherwise the value will be returned.
87+ The callable is only called when the result of :meth: `~object.__anext__ `
88+ is awaited.
89+
90+ *stop_exception * is an exception class or a tuple of exception classes.
91+ If *stop_value * is not specified,
92+ the iteration stops only when the callable raises an exception.
93+ If the callable raises :exc: `StopAsyncIteration `
94+ which does not match *stop_exception *,
95+ it is replaced with a :exc: `RuntimeError `,
96+ as for asynchronous generators (see :pep: `525 `).
97+
98+ For example, reading fixed-size chunks from an asynchronous stream
99+ until the end of file is reached::
68100
69- Return an :term: `asynchronous iterator ` for an :term: `asynchronous iterable `.
70- Equivalent to calling ``x.__aiter__() ``.
101+ from functools import partial
102+ async for chunk in aiter(partial(reader.read, 1024), b''):
103+ process_chunk(chunk)
104+
105+ Or consuming an :class: `asyncio.Queue ` until it is shut down::
71106
72- Note: Unlike :func: `iter `, :func: `aiter ` has no 2-argument variant.
107+ from asyncio import QueueShutDown
108+ async for item in aiter(queue.get, stop_exception=QueueShutDown):
109+ process_item(item)
73110
74111 .. versionadded :: 3.10
75112
113+ .. versionchanged :: next
114+ Added the *stop_value * and *stop_exception * parameters.
115+
76116.. function :: all(iterable, /)
77117
78118 Return ``True `` if all elements of the *iterable * are true (or if the iterable
@@ -89,7 +129,7 @@ are always available. They are listed here in alphabetical order.
89129 anext(async_iterator, default, /)
90130
91131 When awaited, return the next item from the given :term: `asynchronous
92- iterator `, or *default * if given and the iterator is exhausted.
132+ iterator `, or *default * if given and the iterator is :term: ` exhausted ` .
93133
94134 This is the async variant of the :func: `next ` builtin, and behaves
95135 similarly.
@@ -1143,22 +1183,34 @@ are always available. They are listed here in alphabetical order.
11431183
11441184
11451185.. function :: iter(iterable, /)
1146- iter(callable, sentinel, /)
1186+ iter(callable, /, stop_value, *, stop_exception=StopIteration)
1187+ iter(callable, /, *, stop_exception)
11471188
11481189 Return an :term: `iterator ` object. The first argument is interpreted very
1149- differently depending on the presence of the second argument . Without a
1150- second argument , the single argument must be a collection object which supports the
1190+ differently depending on the presence of the other arguments . Without other
1191+ arguments , the single argument must be a collection object which supports the
11511192 :term: `iterable ` protocol (the :meth: `~object.__iter__ ` method),
11521193 or it must support
11531194 the sequence protocol (the :meth: `~object.__getitem__ ` method with integer arguments
11541195 starting at ``0 ``). If it does not support either of those protocols,
1155- :exc: `TypeError ` is raised. If the second argument, *sentinel *, is given,
1196+ :exc: `TypeError ` is raised.
1197+
1198+ If *stop_value * or *stop_exception * is given,
11561199 then the first argument must be a callable object. The iterator created in this case
11571200 will call *callable * with no arguments for each call to its
11581201 :meth: `~iterator.__next__ ` method; if the value returned is equal to
1159- *sentinel *, :exc: `StopIteration ` will be raised, otherwise the value will
1202+ *stop_value *, or if the call raises an exception matching *stop_exception *,
1203+ :exc: `StopIteration ` will be raised, otherwise the value will
11601204 be returned.
11611205
1206+ *stop_exception * is an exception class or a tuple of exception classes.
1207+ If *stop_value * is not specified,
1208+ the iteration stops only when the callable raises an exception.
1209+ If the callable raises :exc: `StopIteration `
1210+ which does not match *stop_exception *,
1211+ it is replaced with a :exc: `RuntimeError `,
1212+ as for generators (see :pep: `479 `).
1213+
11621214 See also :ref: `typeiter `.
11631215
11641216 One useful application of the second form of :func: `iter ` is to build a
@@ -1170,6 +1222,19 @@ are always available. They are listed here in alphabetical order.
11701222 for block in iter(partial(f.read, 64), b''):
11711223 process_block(block)
11721224
1225+ *stop_exception * is useful for callables
1226+ which report :term: `exhaustion <exhausted> ` by raising an exception
1227+ instead of returning a special value.
1228+ For example, draining a queue::
1229+
1230+ import queue
1231+ for item in iter(input_queue.get_nowait, stop_exception=queue.Empty):
1232+ process_item(item)
1233+
1234+ .. versionchanged :: next
1235+ Added the *stop_exception * parameter
1236+ and allowed passing *stop_value * by keyword.
1237+
11731238
11741239.. function :: len(object, /)
11751240
@@ -1250,7 +1315,7 @@ are always available. They are listed here in alphabetical order.
12501315 yielding the results. If additional *iterables * arguments are passed,
12511316 *function * must take that many arguments and is applied to the items from all
12521317 iterables in parallel. With multiple iterables, the iterator stops when the
1253- shortest iterable is exhausted. If *strict * is ``True `` and one of the
1318+ shortest iterable is :term: ` exhausted ` . If *strict * is ``True `` and one of the
12541319 iterables is exhausted before the others, a :exc: `ValueError ` is raised. For
12551320 cases where the function inputs are already arranged into argument tuples,
12561321 see :func: `itertools.starmap `.
@@ -1332,7 +1397,7 @@ are always available. They are listed here in alphabetical order.
13321397
13331398 Retrieve the next item from the :term: `iterator ` by calling its
13341399 :meth: `~iterator.__next__ ` method. If *default * is given, it is returned
1335- if the iterator is exhausted, otherwise :exc: `StopIteration ` is raised.
1400+ if the iterator is :term: ` exhausted ` , otherwise :exc: `StopIteration ` is raised.
13361401
13371402
13381403.. class :: object()
@@ -1394,7 +1459,8 @@ are always available. They are listed here in alphabetical order.
13941459 already exists), ``'x' `` for exclusive creation, and ``'a' `` for appending
13951460 (which on *some * Unix systems, means that *all * writes append to the end of
13961461 the file regardless of the current seek position). In text mode, if
1397- *encoding * is not specified the encoding used is platform-dependent:
1462+ *encoding * is not specified, UTF-8 is used by default; if
1463+ :ref: `Python UTF-8 Mode <utf8-mode >` is disabled,
13981464 :func: `locale.getencoding ` is called to get the current locale encoding.
13991465 (For reading and writing raw bytes use binary mode and leave
14001466 *encoding * unspecified.) The available modes are:
@@ -1425,7 +1491,7 @@ are always available. They are listed here in alphabetical order.
14251491 argument) return contents as :class: `bytes ` objects without any decoding. In
14261492 text mode (the default, or when ``'t' `` is included in the *mode * argument),
14271493 the contents of the file are returned as :class: `str `, the bytes having been
1428- first decoded using a platform-dependent encoding or using the specified
1494+ first decoded using the default encoding or using the specified
14291495 *encoding * if given.
14301496
14311497 .. note ::
@@ -1454,9 +1520,11 @@ are always available. They are listed here in alphabetical order.
14541520 described above for binary files.
14551521
14561522 *encoding * is the name of the encoding used to decode or encode the file.
1457- This should only be used in text mode. The default encoding is platform
1458- dependent (whatever :func: `locale.getencoding ` returns), but any
1459- :term: `text encoding ` supported by Python can be used.
1523+ This should only be used in text mode. The default encoding is UTF-8;
1524+ if :ref: `Python UTF-8 Mode <utf8-mode >` is disabled, the default is
1525+ platform-dependent (whatever :func: `locale.getencoding ` returns).
1526+ Any :term: `text encoding ` supported by Python can be used, and
1527+ ``encoding="locale" `` specifies the current locale encoding explicitly.
14601528 See the :mod: `codecs ` module for the list of supported encodings.
14611529
14621530 *errors * is an optional string that specifies how encoding and decoding
@@ -1573,6 +1641,10 @@ are always available. They are listed here in alphabetical order.
15731641 .. versionchanged :: 3.11
15741642 The ``'U' `` mode has been removed.
15751643
1644+ .. versionchanged :: 3.15
1645+ UTF-8 is now the default encoding, instead of the
1646+ platform-dependent locale encoding (:pep: `686 `).
1647+
15761648.. function :: ord(character, /)
15771649
15781650 Return the ordinal value of a character.
@@ -2247,7 +2319,7 @@ are always available. They are listed here in alphabetical order.
22472319 the code that prepared these iterables. Python offers three different
22482320 approaches to dealing with this issue:
22492321
2250- * By default, :func: `zip ` stops when the shortest iterable is exhausted.
2322+ * By default, :func: `zip ` stops when the shortest iterable is :term: ` exhausted ` .
22512323 It will ignore the remaining items in the longer iterables, cutting off
22522324 the result to the length of the shortest iterable::
22532325
@@ -2262,7 +2334,7 @@ are always available. They are listed here in alphabetical order.
22622334 [('a', 1), ('b', 2), ('c', 3)]
22632335
22642336 Unlike the default behavior, it raises a :exc: `ValueError ` if one iterable
2265- is exhausted before the others:
2337+ is :term: ` exhausted ` before the others:
22662338
22672339 >>> for item in zip (range (3 ), [' fee' , ' fi' , ' fo' , ' fum' ], strict = True ): # doctest: +SKIP
22682340 ... print (item)
0 commit comments