[Python-checkins] cpython (3.2): Issue #8982: Improve the documentation for the argparse Namespace object.

steven.bethard python-checkins at python.org
Sat Mar 26 19:59:33 CET 2011


http://hg.python.org/cpython/rev/423b50086b67
changeset:   68977:423b50086b67
branch:      3.2
parent:      68975:4390d6939a56
user:        Steven Bethard <steven.bethard at gmail.com>
date:        Sat Mar 26 19:50:06 2011 +0100
summary:
  Issue #8982: Improve the documentation for the argparse Namespace object.

files:
  Doc/library/argparse.rst |  21 ++++++++++++++++-----
  Misc/NEWS                |   2 ++
  2 files changed, 18 insertions(+), 5 deletions(-)


diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -1314,13 +1314,24 @@
    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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -157,6 +157,8 @@
 
 - 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
 -----
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list