命名空间,argparse和用法 [英] Namespace, argparse, and usage
问题描述
这确实是几个问题:
-
argparse是否使用名称空间而不是字典的原因?
Is there a reason argparse uses a namespace instead of a dictionary?
假定我有一个__init__(self, init_method, *args)
的课程. init_method
参数告诉init_function我想初始化类的方式,而arg
参数给出初始化所需的所有参数.对于不同的方法,参数可能有所不同.我应该使用字典还是命名空间?
Assuming I have a class with __init__(self, init_method, *args)
. The init_method
parameter tells the init_function which way I want to initialize the class, while arg
parameter gives all the arguments neccesary for the init. The arguments may be different for different methods. Should I use a dictionary, or a namespace?
假设我使用名称空间,如何将名称空间传递给__init__()
?
Assuming that I use a namespace, how do I pass the namespace to __init__()
?
推荐答案
-
arparse
的设计者显然认为,以如下方式访问参数会更方便
The designers of
arparse
apparently felt it would be more convenient to access arguments as
args.arg_name
而不是
args["arg_name"]
不过,这可能是一个品味问题.我会很高兴地使用字典,特别是考虑到标准库中除了argparse
中的那个类之外,没有Namespace
类.
This might be a matter of taste, though. I would have happily gone with the dictionary, especially given the fact that there is no Namespace
class in the standard library, except for the one in argparse
.
使用字典.
如果您确实要在此处使用命名空间,则可以使用init_function(**args.__dict__)
…,但我不建议这样做.
If you really want a namespace here, you can use init_function(**args.__dict__)
…but I don't recommend it.
这篇关于命名空间,argparse和用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!