summaryrefslogtreecommitdiffstats
path: root/Doc/library/inspect.rst
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2015-05-16 17:45:09 (GMT)
committerYury Selivanov <yselivanov@sprymix.com>2015-05-16 17:45:09 (GMT)
commitb907a513c8285410d93adc2895508985838fe1a7 (patch)
tree80450ad00573547efc68174d32388ce1b140bbfc /Doc/library/inspect.rst
parent1392f71c3927c9d81969200f5dfff9fb832cde0b (diff)
downloadcpython-b907a513c8285410d93adc2895508985838fe1a7.zip
cpython-b907a513c8285410d93adc2895508985838fe1a7.tar.gz
cpython-b907a513c8285410d93adc2895508985838fe1a7.tar.bz2
Issue 24190: Add inspect.BoundArguments.apply_defaults() method.
Diffstat (limited to 'Doc/library/inspect.rst')
-rw-r--r--Doc/library/inspect.rst41
1 files changed, 20 insertions, 21 deletions
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index 444d2be..3ee177d 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -667,27 +667,8 @@ function.
Arguments for which :meth:`Signature.bind` or
:meth:`Signature.bind_partial` relied on a default value are skipped.
- However, if needed, it is easy to include them.
-
- ::
-
- >>> def foo(a, b=10):
- ... pass
-
- >>> sig = signature(foo)
- >>> ba = sig.bind(5)
-
- >>> ba.args, ba.kwargs
- ((5,), {})
-
- >>> for param in sig.parameters.values():
- ... if (param.name not in ba.arguments
- ... and param.default is not param.empty):
- ... ba.arguments[param.name] = param.default
-
- >>> ba.args, ba.kwargs
- ((5, 10), {})
-
+ However, if needed, use :meth:`BoundArguments.apply_defaults` to add
+ them.
.. attribute:: BoundArguments.args
@@ -703,6 +684,24 @@ function.
A reference to the parent :class:`Signature` object.
+ .. method:: BoundArguments.apply_defaults()
+
+ Set default values for missing arguments.
+
+ For variable-positional arguments (``*args``) the default is an
+ empty tuple.
+
+ For variable-keyword arguments (``**kwargs``) the default is an
+ empty dict.
+
+ ::
+
+ >>> def foo(a, b='ham', *args): pass
+ >>> ba = inspect.signature(foo).bind('spam')
+ >>> ba.apply_defaults()
+ >>> ba.arguments
+ OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])
+
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
functions::