summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2011-03-26 19:55:36 +0100
committerSteven Bethard <steven.bethard@gmail.com>2011-03-26 19:55:36 +0100
commit0a3a7d35ca7f3a8a1de84cdc7b494c48afd8e0f9 (patch)
tree7e23b1ee59b725d9f5e78f9ca020223285a767ff
parent08ce60800e58db6817f70a80f5297d25ec3fd4ec (diff)
parentd8f2d50c204442d0dfafcc29db3177d15abb41df (diff)
downloadcpython-git-0a3a7d35ca7f3a8a1de84cdc7b494c48afd8e0f9.tar.gz
Issue #8982: Improve the documentation for the argparse Namespace object. (Merge from 3.2.)
-rw-r--r--Doc/library/argparse.rst21
-rw-r--r--Misc/NEWS2
2 files changed, 18 insertions, 5 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index 7a89654c79..da7565c7fd 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -1330,13 +1330,24 @@ of :data:`sys.argv`. This can be accomplished by passing a list of strings to
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])
-Custom namespaces
-^^^^^^^^^^^^^^^^^
+The Namespace object
+^^^^^^^^^^^^^^^^^^^^
+
+By default, :meth:`parse_args` will return a new object of type :class:`Namespace`
+where the necessary attributes have been set. This class is deliberately simple,
+just an :class:`object` subclass with a readable string representation. If you
+prefer to have dict-like view of the attributes, you can use the standard Python
+idiom via :func:`vars`::
+
+ >>> parser = argparse.ArgumentParser()
+ >>> parser.add_argument('--foo')
+ >>> args = parser.parse_args(['--foo', 'BAR'])
+ >>> vars(args)
+ {'foo': 'BAR'}
It may also be useful to have an :class:`ArgumentParser` assign attributes to an
-already existing object, rather than the newly-created :class:`Namespace` object
-that is normally used. This can be achieved by specifying the ``namespace=``
-keyword argument::
+already existing object, rather than a new :class:`Namespace` object. This can
+be achieved by specifying the ``namespace=`` keyword argument::
>>> class C:
... pass
diff --git a/Misc/NEWS b/Misc/NEWS
index 028d248708..6ab63084e1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -299,6 +299,8 @@ Library
- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
+- Issue #8982: Improve the documentation for the argparse Namespace object.
+
Build
-----