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

Skip to content

Commit d8f2d50

Browse files
committed
Issue #8982: Improve the documentation for the argparse Namespace object.
1 parent c13d454 commit d8f2d50

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

Doc/library/argparse.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,13 +1314,24 @@ of :data:`sys.argv`. This can be accomplished by passing a list of strings to
13141314
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
13151315

13161316

1317-
Custom namespaces
1318-
^^^^^^^^^^^^^^^^^
1317+
The Namespace object
1318+
^^^^^^^^^^^^^^^^^^^^
1319+
1320+
By default, :meth:`parse_args` will return a new object of type :class:`Namespace`
1321+
where the necessary attributes have been set. This class is deliberately simple,
1322+
just an :class:`object` subclass with a readable string representation. If you
1323+
prefer to have dict-like view of the attributes, you can use the standard Python
1324+
idiom via :func:`vars`::
1325+
1326+
>>> parser = argparse.ArgumentParser()
1327+
>>> parser.add_argument('--foo')
1328+
>>> args = parser.parse_args(['--foo', 'BAR'])
1329+
>>> vars(args)
1330+
{'foo': 'BAR'}
13191331

13201332
It may also be useful to have an :class:`ArgumentParser` assign attributes to an
1321-
already existing object, rather than the newly-created :class:`Namespace` object
1322-
that is normally used. This can be achieved by specifying the ``namespace=``
1323-
keyword argument::
1333+
already existing object, rather than a new :class:`Namespace` object. This can
1334+
be achieved by specifying the ``namespace=`` keyword argument::
13241335

13251336
>>> class C:
13261337
... pass

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Library
157157

158158
- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
159159

160+
- Issue #8982: Improve the documentation for the argparse Namespace object.
161+
160162
Build
161163
-----
162164

0 commit comments

Comments
 (0)