]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #8982: Improve the documentation for the argparse Namespace object. (Merge...
authorSteven Bethard <steven.bethard@gmail.com>
Sat, 26 Mar 2011 18:59:02 +0000 (19:59 +0100)
committerSteven Bethard <steven.bethard@gmail.com>
Sat, 26 Mar 2011 18:59:02 +0000 (19:59 +0100)
Doc/library/argparse.rst
Misc/NEWS

index ce21442c3f91f296cd82bef280725b5554dec42f..459647b49c8e6bfe3d9bdcdec93e86b72cf18e35 100644 (file)
@@ -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
index fe4c3ae25a9efa4124847c832dfd72274b51c382..3ed93da78104fa8b87623ac3aaf8fe71bbda34a7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -235,6 +235,10 @@ Library
 - Issue #9509: argparse now properly handles IOErrors raised by
   argparse.FileType.
 
+- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
+
+- Issue #8982: Improve the documentation for the argparse Namespace object.
+
 Extension Modules
 -----------------
 
@@ -1343,8 +1347,6 @@ Library
 - Issue #8374: Update the internal alias table in the :mod:`locale` module to
   cover recent locale changes and additions.
 
-- Issue #9348: Raise an early error if argparse nargs and metavar don't match.
-
 Extension Modules
 -----------------