@@ -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
@@ -985,18 +991,20 @@ function.
985
991
For variable-keyword arguments (``**kwargs ``) the default is an
986
992
empty dict.
987
993
988
- ::
994
+ .. doctest ::
989
995
990
- >>> def foo(a, b='ham', *args): pass
991
- >>> ba = inspect.signature(foo).bind('spam')
992
- >>> ba.apply_defaults()
993
- >>> ba.arguments
994
- {'a': 'spam', 'b': 'ham', 'args': ()}
996
+ >>> def foo (a , b = ' ham' , * args ): pass
997
+ >>> ba = inspect.signature(foo).bind(' spam' )
998
+ >>> ba.apply_defaults()
999
+ >>> ba.arguments
1000
+ {'a': 'spam', 'b': 'ham', 'args': ()}
995
1001
996
1002
.. versionadded :: 3.5
997
1003
998
1004
The :attr: `args ` and :attr: `kwargs ` properties can be used to invoke
999
- functions::
1005
+ functions:
1006
+
1007
+ .. testcode ::
1000
1008
1001
1009
def test(a, *, b):
1002
1010
...
@@ -1115,20 +1123,22 @@ Classes and functions
1115
1123
``** `` arguments, if any) to their values from *args * and *kwds *. In case of
1116
1124
invoking *func * incorrectly, i.e. whenever ``func(*args, **kwds) `` would raise
1117
1125
an exception because of incompatible signature, an exception of the same type
1118
- and the same or similar message is raised. For example::
1119
-
1120
- >>> from inspect import getcallargs
1121
- >>> def f(a, b=1, *pos, **named):
1122
- ... pass
1123
- ...
1124
- >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
1125
- True
1126
- >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
1127
- True
1128
- >>> getcallargs(f)
1129
- Traceback (most recent call last):
1130
- ...
1131
- TypeError: f() missing 1 required positional argument: 'a'
1126
+ and the same or similar message is raised. For example:
1127
+
1128
+ .. doctest ::
1129
+
1130
+ >>> from inspect import getcallargs
1131
+ >>> def f (a , b = 1 , * pos , ** named ):
1132
+ ... pass
1133
+ ...
1134
+ >>> getcallargs(f, 1 , 2 , 3 ) == {' a' : 1 , ' named' : {}, ' b' : 2 , ' pos' : (3 ,)}
1135
+ True
1136
+ >>> getcallargs(f, a = 2 , x = 4 ) == {' a' : 2 , ' named' : {' x' : 4 }, ' b' : 1 , ' pos' : ()}
1137
+ True
1138
+ >>> getcallargs(f)
1139
+ Traceback (most recent call last):
1140
+ ...
1141
+ TypeError: f() missing 1 required positional argument: 'a'
1132
1142
1133
1143
.. versionadded :: 3.2
1134
1144
0 commit comments