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.

classification
Title: Iterable argparse Namespace
Type: Stage: resolved
Components: Documentation Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: argparse docs cross reference Namespace as a class but the Namespace class is not documented
View: 8982
Assigned To: docs@python Nosy List: Hobson.Lane, Jean-Lou.Dupont, asvetlov, bethard, chris.jerdonek, docs@python, eric.araujo, eric.snow, python-dev, rhettinger, vdupras
Priority: normal Keywords: patch

Created on 2011-01-31 08:05 by vdupras, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue11076.diff vdupras, 2011-02-02 15:12 review
issue11076_doc_only.diff vdupras, 2011-02-10 17:24 review
Messages (14)
msg127582 - (view) Author: Virgil Dupras (vdupras) (Python triager) Date: 2011-01-31 08:05
Currently, there is no (documented) way to easily extract arguments in an argparse Namespace as a dictionary. This way, it would me easy to interface a function taking a lot of kwargs like this:

>>> args = parser.parse_args()
>>> my_function(**dict(args))

There's "_get_kwargs()" but it's a private undocumented method. I guess that making it public would be problematic because of the namespace pollution that would occur. That's why I'm proposing to make it iterable.

If it isn't rejected, I'd gladly work on the required patch.
msg127588 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-31 09:56
+1 for making the object readily convertible to a dictionary.  That would also serve to make it more introspectable without losing the simplicity of the current design.
msg127734 - (view) Author: Virgil Dupras (vdupras) (Python triager) Date: 2011-02-02 15:12
I went ahead and created a patch (with test and doc) making argparse.Namespace iterable.
msg127916 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-02-04 18:43
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).
msg127946 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-02-04 21:56
What's wrong with `vars(args)`? That's the standard way of getting a dict from an object, no?

Note that you're not always guaranteed to get a Namespace back (e.g. if you pass the namespace= argument to parse_args), and I'd generally like to keep Namespace as simple as possible.
msg127978 - (view) Author: Virgil Dupras (vdupras) (Python triager) Date: 2011-02-05 11:02
I didn't know about vars() (well, I knew it existed, but never was quite sure what it did).

Given that I'm not a Python newbie, I'm guessing I'm not alone in this situation. Maybe that instead of making the Namespace iterable, we should just add an example usage of vars() in argparse's documentation?
msg128046 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-02-06 09:40
Yes, definitely `vars` deserves some description and an example in the documentation. This isn't the first time this question has come up. =)
msg128317 - (view) Author: Virgil Dupras (vdupras) (Python triager) Date: 2011-02-10 17:24
Here's a documentation-only patch which adds a section about using vars() to convert a namespace to a dict.

If this becomes a documentation issue, can we target Python 3.2.1 instead of Python 3.3?
msg128346 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-02-10 22:12
+1 for this patch.

Documentation fixes are backported from the development branch to stable branches, so yes, this will land in 3.1 too.
msg128371 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-02-11 10:48
There's no argparse in 3.1, so it should only go into 2.7, 3.2 and 3.3. But yes, the patch looks great to me too.
msg175483 - (view) Author: Hobs (Hobson.Lane) Date: 2012-11-13 00:52
Seems like a great idea. `foo(**dict(args))` is very useful.

I tested `foo(**dict(iter(o.__dict__.items())))` on python 2.7 Mac OSX for my foo and it worked well.
msg176555 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-11-28 17:18
New changeset bbecbcff0ce4 by Andrew Svetlov in branch '3.2':
Issue #11076: document the way to convert argparse.Namespace to a dict.
http://hg.python.org/cpython/rev/bbecbcff0ce4

New changeset ee4e31845977 by Andrew Svetlov in branch '3.3':
Merge issue #11076: document the way to convert argparse.Namespace to a dict.
http://hg.python.org/cpython/rev/ee4e31845977

New changeset 63ff2d421d1a by Andrew Svetlov in branch 'default':
Merge issue #11076: document the way to convert argparse.Namespace to a dict.
http://hg.python.org/cpython/rev/63ff2d421d1a
msg176557 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-11-28 17:26
Sorry, looks like it work already was done. Reverting: c008f070f88a 814403d824a5 ddcf09a348ca
msg176566 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-11-28 18:34
Following up Andrew's last comment, for the record, it looks like this was done as part of issue 8982.
History
Date User Action Args
2022-04-11 14:57:12adminsetgithub: 55285
2012-11-28 18:34:11chris.jerdoneksetsuperseder: argparse docs cross reference Namespace as a class but the Namespace class is not documented

messages: + msg176566
nosy: + chris.jerdonek
2012-11-28 17:26:42asvetlovsetstatus: open -> closed

nosy: + asvetlov
messages: + msg176557

resolution: duplicate
stage: patch review -> resolved
2012-11-28 17:18:36python-devsetnosy: + python-dev
messages: + msg176555
2012-11-13 01:21:49eric.snowsetnosy: + eric.snow
2012-11-13 00:52:34Hobson.Lanesetnosy: + Hobson.Lane
messages: + msg175483
2012-01-20 19:06:28Jean-Lou.Dupontsetnosy: + Jean-Lou.Dupont
2011-02-11 10:48:36bethardsetnosy: rhettinger, bethard, vdupras, eric.araujo, docs@python
messages: + msg128371
versions: - Python 3.1
2011-02-10 22:12:43eric.araujosetassignee: docs@python
type: enhancement ->
components: + Documentation, - Library (Lib)
versions: + Python 3.1, Python 2.7, Python 3.2
nosy: + docs@python

messages: + msg128346
2011-02-10 17:24:45vduprassetfiles: + issue11076_doc_only.diff
nosy: rhettinger, bethard, vdupras, eric.araujo
messages: + msg128317
2011-02-06 09:40:06bethardsetnosy: rhettinger, bethard, vdupras, eric.araujo
messages: + msg128046
2011-02-05 11:02:29vduprassetnosy: rhettinger, bethard, vdupras, eric.araujo
messages: + msg127978
2011-02-04 21:56:01bethardsetnosy: rhettinger, bethard, vdupras, eric.araujo
messages: + msg127946
2011-02-04 18:43:08eric.araujosetnosy: + eric.araujo, bethard

messages: + msg127916
stage: patch review
2011-02-02 15:12:28vduprassetfiles: + issue11076.diff

messages: + msg127734
keywords: + patch, - easy
2011-01-31 09:56:06rhettingersetnosy: + rhettinger
messages: + msg127588
2011-01-31 08:05:02vduprascreate