相关疑难解决方法(0)

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

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

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__', '_ …
Run Code Online (Sandbox Code Playgroud)

python dictionary duck-typing

201
推荐指数
2
解决办法
11万
查看次数

标签 统计

dictionary ×1

duck-typing ×1

python ×1