如何创建Python名称空间(argparse.parse_args值)? [英] How do I create a Python namespace (argparse.parse_args value)?
问题描述
要交互式测试我的python脚本,我想创建一个Namespace
对象,类似于argparse.parse_args()
返回的对象.
显而易见的方式,
To interactively test my python script, I would like to create a Namespace
object, similar to what would be returned by argparse.parse_args()
.
The obvious way,
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.parse_args()
Namespace()
>>> parser.parse_args("-a")
usage: [-h]
: error: unrecognized arguments: - a
Process Python exited abnormally with code 2
可能会由于一个愚蠢的错误而导致Python repl退出(如上所述).
may result in Python repl exiting (as above) on a silly error.
那么,用给定的属性集创建Python名称空间的最简单方法是什么?
例如,我可以即时创建dict
(dict([("a",1),("b","c")])
),但不能将其用作Namespace
:
E.g., I can create a dict
on the fly (dict([("a",1),("b","c")])
) but I cannot use it as a Namespace
:
AttributeError: 'dict' object has no attribute 'a'
推荐答案
您可以创建一个简单的类:
You can create a simple class:
class Namespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
,它在属性方面的作用与argparse
Namespace
类完全相同:
and it'll work the exact same way as the argparse
Namespace
class when it comes to attributes:
>>> args = Namespace(a=1, b='c')
>>> args.a
1
>>> args.b
'c'
或者,只需导入类;它可以从argparse
模块中获得:
Alternatively, just import the class; it is available from the argparse
module:
from argparse import Namespace
args = Namespace(a=1, b='c')
从Python 3.3开始,还有 types.SimpleNamespace
,实际上执行相同的操作:
As of Python 3.3, there is also types.SimpleNamespace
, which essentially does the same thing:
>>> from types import SimpleNamespace
>>> args = SimpleNamespace(a=1, b='c')
>>> args.a
1
>>> args.b
'c'
这两种类型是不同的; SimpleNamespace
主要用于sys.implementation
属性和time.get_clock_info()
的返回值.
The two types are distinct; SimpleNamespace
is primarily used for the sys.implementation
attribute and the return value of time.get_clock_info()
.
其他比较:
- 两个类都支持相等性测试;对于相同类的两个实例,如果
instance_a == instance_b
具有相同的属性且具有相同的值,则为instance_a == instance_b
. - 两个类都有一个有用的
__repr__
来显示它们具有的属性. -
Namespace()
对象支持容器测试;如果名称空间实例具有名称为attrname
的属性,则'attrname' in instance
为true.SimpleNamespace
不. -
Namespace()
对象具有未公开的._get_kwargs()
方法,该方法返回该实例的(name, value)
属性的排序列表.您可以使用sorted(vars(instance).items())
对于任一类都获得相同的结果. - 虽然
SimpleNamespace()
是用C实现的,而Namespace()
是用Python实现的,但是属性访问并没有更快,因为它们都使用相同的__dict__
存储属性.对于SimpleNamespace()
实例,相等性测试和生成表示形式要快一些.
- Both classes support equality testing; for two instances of the same class,
instance_a == instance_b
is true if they have the same attributes with the same values. - Both classes have a helpful
__repr__
to show what attributes they have. Namespace()
objects support containment testing;'attrname' in instance
is true if the namespace instance has an attribute namendattrname
.SimpleNamespace
does not.Namespace()
objects have an undocumented._get_kwargs()
method that returns a sorted list of(name, value)
attributes for that instance. You can get the same for either class usingsorted(vars(instance).items())
.- While
SimpleNamespace()
is implemented in C andNamespace()
is implemented in Python, attribute access is no faster because both use the same__dict__
storage for the attributes. Equality testing and producing the representation are a little faster forSimpleNamespace()
instances.
这篇关于如何创建Python名称空间(argparse.parse_args值)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!