(original) (raw)

changeset: 68979:fe72160ea2a3 branch: 2.7 parent: 68969:4bb651eb7539 user: Steven Bethard steven.bethard@gmail.com date: Sat Mar 26 19:59:02 2011 +0100 files: Doc/library/argparse.rst Misc/NEWS description: Issue #8982: Improve the documentation for the argparse Namespace object. (Merge from 3.2.) diff -r 4bb651eb7539 -r fe72160ea2a3 Doc/library/argparse.rst --- a/Doc/library/argparse.rst Sat Mar 26 17:57:52 2011 +0100 +++ b/Doc/library/argparse.rst Sat Mar 26 19:59:02 2011 +0100 @@ -1310,13 +1310,24 @@ Namespace(accumulate=, 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 diff -r 4bb651eb7539 -r fe72160ea2a3 Misc/NEWS --- a/Misc/NEWS Sat Mar 26 17:57:52 2011 +0100 +++ b/Misc/NEWS Sat Mar 26 19:59:02 2011 +0100 @@ -235,6 +235,10 @@ - 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 @@ - 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 -----------------/steven.bethard@gmail.com