Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e15cb61

Browse files
committed
Issue #11076: document the way to convert argparse.Namespace to a dict.
Initial patch by Virgil Dupras.
1 parent c2a7fd6 commit e15cb61

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Doc/library/argparse.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,21 @@ be achieved by specifying the ``namespace=`` keyword argument::
14251425
'BAR'
14261426

14271427

1428+
Converting the namespace to a dict
1429+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1430+
1431+
It's possible to convert a namespace to a :class:`dict` by using the built-in
1432+
function :func:`vars` in this fashion::
1433+
1434+
args = parser.parse_args()
1435+
argdict = vars(args)
1436+
1437+
This makes it easy to introspect the namespace or to pass the command-line
1438+
arguments to a function taking a bunch of keyword arguments::
1439+
1440+
somefunction(**vars(parser.parse_args()))
1441+
1442+
14281443
Other utilities
14291444
---------------
14301445

0 commit comments

Comments
 (0)