This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.araujo
Recipients bethard, eric.araujo, rhettinger, vdupras
Date 2011-02-04.18:43:08
SpamBayes Score 8.223144e-12
Marked as misclassified No
Message-id <1296844989.5.0.346628925387.issue11076@psf.upfronthosting.co.za>
In-reply-to
Content
Nice idea indeed, thanks for the patch.

For the doc section, I’d prefer to de-emplasize the specific use case of **kwargs, in favor of mentioning dict conversion in a general way:

  Converting the namespace to a dict
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  
  Namespace objects are iterable so you can easily convert them to a
  :class:`dict`::

     args = parser.parse_args()
     argdict = dict(args)

  This makes it easy to introspect the namespace or to pass the
  command-line arguments to a function taking a bunch of keyword
  arguments::

     somefunction(**dict(parser.parse_args()))

 
+    def __iter__(self):
+        return iter(self.__dict__.items())
Isn’t “return self.__dict__.items()” sufficient in 3.x?


Alternate idea: don’t implement __iter__, which is sorta too broad, and implement a method that just returns a dict.

Musing: Isn’t Namespace (technically: _AttributeHolder) very much like a named tuple?  Could some code be removed by using named tuples in argparse?  Note that named tuples provide a method to convert themselves to a dict, and are efficient (IIRC).
History
Date User Action Args
2011-02-04 18:43:09eric.araujosetrecipients: + eric.araujo, rhettinger, bethard, vdupras
2011-02-04 18:43:09eric.araujosetmessageid: <1296844989.5.0.346628925387.issue11076@psf.upfronthosting.co.za>
2011-02-04 18:43:08eric.araujolinkissue11076 messages
2011-02-04 18:43:08eric.araujocreate