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

查看:25
本文介绍了命名空间、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 参数给出了 init 所需的所有参数.不同方法的参数可能不同.我应该使用字典还是命名空间?

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

不过,这可能是一个品味问题.我很乐意使用字典,特别是考虑到标准库中没有 Namespace 类,除了 argparse 中的一个.

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天全站免登陆