什么是命名空间对象? [英] What is a namespace object?

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

问题描述

import argparse

parser = argparse.ArgumentParser(description='sort given numbers')
parser.add_argument('-s', nargs = '+', type = int)
args = parser.parse_args()
print(args)

当我运行命令时在命令行上

On command line when I run the command

python3 file_name.py -s 9 8 76

它打印命名空间(s=[9, 8, 76]).

如何访问列表 [9, 8, 76]?什么是命名空间对象.我在哪里可以了解更多信息?

How can I access the list [9, 8, 76]? What is the namespace object. Where can I learn more about it?

推荐答案

  • argparse.Namespace 的文档可以在 这里.
  • 您可以通过执行 args.s 来访问 s 属性.
  • 如果你想以字典的形式访问它,你可以做 vars(args),这意味着你也可以做 vars(args)['s']
    • The documentation for argparse.Namespace can be found here.
    • You can access the s attribute by doing args.s.
    • If you'd like to access this as a dictionary, you can do vars(args), which means you can also do vars(args)['s']
    • 这篇关于什么是命名空间对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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