summaryrefslogtreecommitdiff
path: root/Doc
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2011-03-26 19:59:02 +0100
committerSteven Bethard <steven.bethard@gmail.com>2011-03-26 19:59:02 +0100
commit589582f4228aba4e19593a7589ae39a3c5f46b7d (patch)
tree695098f20affd094bba76b568491030b9c608503 /Doc
parent732d21493100eb0309c7e0b8d60d1a217417bc50 (diff)
downloadcpython-589582f4228aba4e19593a7589ae39a3c5f46b7d.tar.gz
Issue #8982: Improve the documentation for the argparse Namespace object. (Merge from 3.2.)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/argparse.rst21
1 files changed, 16 insertions, 5 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index ce21442c3f..459647b49c 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -1310,13 +1310,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(object):
... pass