@@ -392,7 +392,11 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
392
392
Return ``True `` if the object can be used in :keyword: `await ` expression.
393
393
394
394
Can also be used to distinguish generator-based coroutines from regular
395
- generators::
395
+ generators:
396
+
397
+ .. testcode ::
398
+
399
+ import types
396
400
397
401
def gen():
398
402
yield
@@ -409,13 +413,15 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
409
413
.. function :: isasyncgenfunction(object)
410
414
411
415
Return ``True `` if the object is an :term: `asynchronous generator ` function,
412
- for example::
416
+ for example:
413
417
414
- >>> async def agen():
415
- ... yield 1
416
- ...
417
- >>> inspect.isasyncgenfunction(agen)
418
- True
418
+ .. doctest ::
419
+
420
+ >>> async def agen ():
421
+ ... yield 1
422
+ ...
423
+ >>> inspect.isasyncgenfunction(agen)
424
+ True
419
425
420
426
.. versionadded :: 3.6
421
427
@@ -968,18 +974,20 @@ function.
968
974
For variable-keyword arguments (``**kwargs ``) the default is an
969
975
empty dict.
970
976
971
- ::
977
+ .. doctest ::
972
978
973
- >>> def foo(a, b='ham', *args): pass
974
- >>> ba = inspect.signature(foo).bind('spam')
975
- >>> ba.apply_defaults()
976
- >>> ba.arguments
977
- {'a': 'spam', 'b': 'ham', 'args': ()}
979
+ >>> def foo (a , b = ' ham' , * args ): pass
980
+ >>> ba = inspect.signature(foo).bind(' spam' )
981
+ >>> ba.apply_defaults()
982
+ >>> ba.arguments
983
+ {'a': 'spam', 'b': 'ham', 'args': ()}
978
984
979
985
.. versionadded :: 3.5
980
986
981
987
The :attr: `args ` and :attr: `kwargs ` properties can be used to invoke
982
- functions::
988
+ functions:
989
+
990
+ .. testcode ::
983
991
984
992
def test(a, *, b):
985
993
...
@@ -1098,20 +1106,22 @@ Classes and functions
1098
1106
``** `` arguments, if any) to their values from *args * and *kwds *. In case of
1099
1107
invoking *func * incorrectly, i.e. whenever ``func(*args, **kwds) `` would raise
1100
1108
an exception because of incompatible signature, an exception of the same type
1101
- and the same or similar message is raised. For example::
1102
-
1103
- >>> from inspect import getcallargs
1104
- >>> def f(a, b=1, *pos, **named):
1105
- ... pass
1106
- ...
1107
- >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
1108
- True
1109
- >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
1110
- True
1111
- >>> getcallargs(f)
1112
- Traceback (most recent call last):
1113
- ...
1114
- TypeError: f() missing 1 required positional argument: 'a'
1109
+ and the same or similar message is raised. For example:
1110
+
1111
+ .. doctest ::
1112
+
1113
+ >>> from inspect import getcallargs
1114
+ >>> def f (a , b = 1 , * pos , ** named ):
1115
+ ... pass
1116
+ ...
1117
+ >>> getcallargs(f, 1 , 2 , 3 ) == {' a' : 1 , ' named' : {}, ' b' : 2 , ' pos' : (3 ,)}
1118
+ True
1119
+ >>> getcallargs(f, a = 2 , x = 4 ) == {' a' : 2 , ' named' : {' x' : 4 }, ' b' : 1 , ' pos' : ()}
1120
+ True
1121
+ >>> getcallargs(f)
1122
+ Traceback (most recent call last):
1123
+ ...
1124
+ TypeError: f() missing 1 required positional argument: 'a'
1115
1125
1116
1126
.. versionadded :: 3.2
1117
1127
0 commit comments