什么是名称空间对象? [英] What is a namespace object?
本文介绍了什么是名称空间对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
它打印
Namespace(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 doingargs.s
. - If you'd like to access this as a dictionary, you can do
vars(args)
, which means you can also dovars(args)['s']
这篇关于什么是名称空间对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文