Index: Doc/library/argparse.rst =================================================================== --- Doc/library/argparse.rst (revision 88318) +++ Doc/library/argparse.rst (working copy) @@ -1314,6 +1314,17 @@ Namespace(accumulate=, integers=[1, 2, 3, 4]) +Passing the namespace as ``kwargs`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the arguments you're parsing are to be directly passed to a function taking +a bunch of keyword arguments, you can quickly pass your namespace to that +function by converting it into a dictionary:: + + >>> args = parser.parse_args() + >>> myfunction(**dict(args)) + + Custom namespaces ^^^^^^^^^^^^^^^^^ Index: Lib/argparse.py =================================================================== --- Lib/argparse.py (revision 88318) +++ Lib/argparse.py (working copy) @@ -1172,6 +1172,8 @@ def __contains__(self, key): return key in self.__dict__ + def __iter__(self): + return iter(self.__dict__.items()) class _ActionsContainer(object): Index: Lib/test/test_argparse.py =================================================================== --- Lib/test/test_argparse.py (revision 88318) +++ Lib/test/test_argparse.py (working copy) @@ -4318,6 +4318,12 @@ self.assertTrue(ns2 != ns3) self.assertTrue(ns2 != ns4) + def test_iter(self): + # Namespace is iterable and yields its attribute/value pairs (in no special order). + ns = argparse.Namespace(foo='bar', spam='eggs') + + self.assertEqual(dict(ns), {'foo': 'bar', 'spam': 'eggs'}) + # =================== # File encoding tests