将Python argparse.Namespace()视为字典的正确方法是什么?

问题:将Python argparse.Namespace()视为字典的正确方法是什么?

如果我想将的结果(argparse.ArgumentParser()Namespace对象)与需要字典或类映射对象的方法一起使用(请参阅collections.Mapping),那么正确的方法是什么?

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']

“进入”对象并使用其__dict__属性是否合适?

我认为答案是否定的:__dict__闻起来像实现的约定,而不是接口的含义__getattribute____setattr__或者说__contains__是。

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.


回答 0

您可以使用vars()访问命名空间的字典:

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

您可以根据需要直接修改字典:

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

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

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.


回答 1

从马口直行

如果您更喜欢具有类似dict的属性视图,则可以使用标准的Python习惯用法vars()

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> args = parser.parse_args(['--foo', 'BAR'])
>>> vars(args)
{'foo': 'BAR'}

— Python标准库,16.4.4.6。命名空间对象

Straight from the horse’s mouth:

If you prefer to have dict-like view of the attributes, you can use the standard Python idiom, vars():

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> args = parser.parse_args(['--foo', 'BAR'])
>>> vars(args)
{'foo': 'BAR'}

— The Python Standard Library, 16.4.4.6. The Namespace object


回答 2

“进入”对象并使用其dict属性是否合适?

通常,我会说“不”。但是Namespace,令我震惊的是过度设计,可能是因为类无法从内置类型继承。

另一方面,Namespace确实提供了一种针对argparse的面向任务的方法,我想不出需要抓住的情况__dict__,但是我的想象力的极限与您的一样。

Is it proper to “reach into” an object and use its dict property?

In general, I would say “no”. However Namespace has struck me as over-engineered, possibly from when classes couldn’t inherit from built-in types.

On the other hand, Namespace does present a task-oriented approach to argparse, and I can’t think of a situation that would call for grabbing the __dict__, but the limits of my imagination are not the same as yours.