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.
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:12 | admin | set | github: 55285 |
| 2012年11月28日 18:34:11 | chris.jerdonek | set | superseder: 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:42 | asvetlov | set | status: open -> closed nosy: + asvetlov messages: + msg176557 resolution: duplicate stage: patch review -> resolved |
| 2012年11月28日 17:18:36 | python-dev | set | nosy:
+ python-dev messages: + msg176555 |
| 2012年11月13日 01:21:49 | eric.snow | set | nosy:
+ eric.snow |
| 2012年11月13日 00:52:34 | Hobson.Lane | set | nosy:
+ Hobson.Lane messages: + msg175483 |
| 2012年01月20日 19:06:28 | Jean-Lou.Dupont | set | nosy:
+ Jean-Lou.Dupont |
| 2011年02月11日 10:48:36 | bethard | set | nosy:
rhettinger, bethard, vdupras, eric.araujo, docs@python messages: + msg128371 versions: - Python 3.1 |
| 2011年02月10日 22:12:43 | eric.araujo | set | assignee: 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:45 | vdupras | set | files:
+ issue11076_doc_only.diff nosy: rhettinger, bethard, vdupras, eric.araujo messages: + msg128317 |
| 2011年02月06日 09:40:06 | bethard | set | nosy:
rhettinger, bethard, vdupras, eric.araujo messages: + msg128046 |
| 2011年02月05日 11:02:29 | vdupras | set | nosy:
rhettinger, bethard, vdupras, eric.araujo messages: + msg127978 |
| 2011年02月04日 21:56:01 | bethard | set | nosy:
rhettinger, bethard, vdupras, eric.araujo messages: + msg127946 |
| 2011年02月04日 18:43:08 | eric.araujo | set | nosy:
+ eric.araujo, bethard messages: + msg127916 stage: patch review |
| 2011年02月02日 15:12:28 | vdupras | set | files:
+ issue11076.diff messages: + msg127734 keywords: + patch, - easy |
| 2011年01月31日 09:56:06 | rhettinger | set | nosy:
+ rhettinger messages: + msg127588 |
| 2011年01月31日 08:05:02 | vdupras | create | |