将 Python argparse.Namespace() 视为字典的正确方法是什么? [英] What is the right way to treat Python argparse.Namespace() as a dictionary?

查看:35
本文介绍了将 Python argparse.Namespace() 视为字典的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用 argparse.ArgumentParser() 的结果,它是一个 Namespace 对象,方法需要一个字典或类似映射的对象(参见 collections.Mapping),正确的做法是什么它吗?

C:>pythonPython 2.7.3(默认,2012 年 4 月 10 日,23:31:26)[MSC v.1500 32 位(英特尔)] on win32输入帮助"、版权"、信用"或许可证"以获取更多信息.>>>导入参数解析>>>args = argparse.Namespace()>>>args.foo = 1>>>args.bar = [1,2,3]>>>args.baz = 'yippee'>>>args['baz']回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:命名空间"对象没有属性__getitem__">>>目录(参数)['__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__','__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_args', '_get_kwargs', 'bar', 'baz', 'foo']

进入"一个对象并使用它的 __dict__ 属性是否合适?

我认为答案是否定的:__dict__ 闻起来像是一个实现约定,但不是接口,就像 __getattribute____setattr____contains__ 似乎是.

解决方案

您可以使用 vars():

<预><代码>>>>导入参数解析>>>args = argparse.Namespace()>>>args.foo = 1>>>args.bar = [1,2,3]>>>d = 变量(参数)>>>d{'foo': 1, 'bar': [1, 2, 3]}

如果你愿意,你可以直接修改字典:

<预><代码>>>>d['baz'] = '存储我'>>>参数文件'储存我'

是的,可以访问 __dict__ 属性.这是一种定义明确、经过测试且有保证的行为.

If I want to use the results of argparse.ArgumentParser(), which is a Namespace object, with a method that expects a dictionary or mapping-like object (see collections.Mapping), what is the right way to do it?

C:>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> args = argparse.Namespace()
>>> args.foo = 1
>>> args.bar = [1,2,3]
>>> args.baz = 'yippee'
>>> args['baz']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Namespace' object has no attribute '__getitem__'
>>> dir(args)
['__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '_
_format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__
', '__str__', '__subclasshook__', '__weakref__', '_get_args', '_get_kwargs', 'ba
r', 'baz', 'foo']

Is it proper to "reach into" an object and use its __dict__ property?

I would think the answer is no: __dict__ smells like a convention for implementation, but not for an interface, the way __getattribute__ or __setattr__ or __contains__ seem to be.

解决方案

You can access the namespace's dictionary with vars():

>>> import argparse
>>> args = argparse.Namespace()
>>> args.foo = 1
>>> args.bar = [1,2,3]
>>> d = vars(args)
>>> d
{'foo': 1, 'bar': [1, 2, 3]}

You can modify the dictionary directly if you wish:

>>> d['baz'] = 'store me'
>>> args.baz
'store me'

Yes, it is okay to access the __dict__ attribute. It is a well-defined, tested, and guaranteed behavior.

这篇关于将 Python argparse.Namespace() 视为字典的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆