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

Skip to content

Commit 0a3a7d3

Browse files
committed
Issue #8982: Improve the documentation for the argparse Namespace object. (Merge from 3.2.)
2 parents 08ce608 + d8f2d50 commit 0a3a7d3

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
@@ -1330,13 +1330,24 @@ of :data:`sys.argv`. This can be accomplished by passing a list of strings to
13301330
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
13311331

13321332

1333-
Custom namespaces
1334-
^^^^^^^^^^^^^^^^^
1333+
The Namespace object
1334+
^^^^^^^^^^^^^^^^^^^^
1335+
1336+
By default, :meth:`parse_args` will return a new object of type :class:`Namespace`
1337+
where the necessary attributes have been set. This class is deliberately simple,
1338+
just an :class:`object` subclass with a readable string representation. If you
1339+
prefer to have dict-like view of the attributes, you can use the standard Python
1340+
idiom via :func:`vars`::
1341+
1342+
>>> parser = argparse.ArgumentParser()
1343+
>>> parser.add_argument('--foo')
1344+
>>> args = parser.parse_args(['--foo', 'BAR'])
1345+
>>> vars(args)
1346+
{'foo': 'BAR'}
13351347

13361348
It may also be useful to have an :class:`ArgumentParser` assign attributes to an
1337-
already existing object, rather than the newly-created :class:`Namespace` object
1338-
that is normally used. This can be achieved by specifying the ``namespace=``
1339-
keyword argument::
1349+
already existing object, rather than a new :class:`Namespace` object. This can
1350+
be achieved by specifying the ``namespace=`` keyword argument::
13401351

13411352
>>> class C:
13421353
... pass

Misc/NEWS

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

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

302+
- Issue #8982: Improve the documentation for the argparse Namespace object.
303+
302304
Build
303305
-----
304306

0 commit comments

Comments
 (0)