]> granicus.if.org Git - python/commitdiff
Issue #8982: Improve the documentation for the argparse Namespace object.
authorSteven Bethard <steven.bethard@gmail.com>
Sat, 26 Mar 2011 18:50:06 +0000 (19:50 +0100)
committerSteven Bethard <steven.bethard@gmail.com>
Sat, 26 Mar 2011 18:50:06 +0000 (19:50 +0100)
Doc/library/argparse.rst
Misc/NEWS

index 8bd3ca53bb5e846adcf0b0cbe5a2832a82901973..102b3e9302542eb53ab16bc8e7d6b225890b429d 100644 (file)
@@ -1314,13 +1314,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
index f5ff837d76920c278236a22e6485efc96cfe240f..5958a325179ca8031544572e298c2c37e940b635 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -157,6 +157,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
 -----