命名空间,argparse和用法 [英] Namespace, argparse, and usage

查看:108
本文介绍了命名空间,argparse和用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这确实是几个问题:

  1. argparse是否使用名称空间而不是字典的原因?

  1. 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__()?

推荐答案

  1. arparse的设计者显然认为,以如下方式访问参数会更方便

  1. 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屋!

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