Message127916
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). |
|
| Date |
User |
Action |
Args |
| 2011-02-04 18:43:09 | eric.araujo | set | recipients:
+ eric.araujo, rhettinger, bethard, vdupras |
| 2011-02-04 18:43:09 | eric.araujo | set | messageid: <1296844989.5.0.346628925387.issue11076@psf.upfronthosting.co.za> |
| 2011-02-04 18:43:08 | eric.araujo | link | issue11076 messages |
| 2011-02-04 18:43:08 | eric.araujo | create | |
|