将argparse名称空间转换为递归命令
我们已经有一些非回收解决方案在这里。
import argparse
args = argparse.Namespace()
args.foo = 1
args.bar = [1,2,3]
args.c = argparse.Namespace()
args.c.foo = 'a'
d = vars(args)
>>> d
{'foo': 1, 'bar': [1, 2, 3], 'c': Namespace(foo='a')}
问题是,如果第二级条目也是一个名称空间,我们实际上得到的是名称空间的命令。
问题是,是否有一个方便的递归解决方案为我们准备。
we already have some non-recursive solutions here.
import argparse
args = argparse.Namespace()
args.foo = 1
args.bar = [1,2,3]
args.c = argparse.Namespace()
args.c.foo = 'a'
d = vars(args)
>>> d
{'foo': 1, 'bar': [1, 2, 3], 'c': Namespace(foo='a')}
The problem is if a second-level entry is also a Namespace, what we actually get is a dict of Namespace.
The question is if there is a handy recursive solution that is ready for us.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有已经制定的递归解决方案,但这是一个简单的解决方案:
I don't think there's an already-made recursive solution, but here's a simple one: