How do I create a Python namespace (argparse.parse_args value)?

前端 未结 3 830
终归单人心
终归单人心 2020-12-04 15:18

To interactively test my python script, I would like to create a Namespace object, similar to what would be returned by argparse.parse_args(). The

3条回答
  •  生来不讨喜
    2020-12-04 15:45

    It is now recommended to use SimpleNamespace from the types module. It does the same thing as the accepted answer except for it will be faster and have a few more builtins such as equals and repr.

    from types import SimpleNamespace
    
    sn = SimpleNamespace()
    sn.a = 'test'
    sn.a
    
    # output
    'test'
    

提交回复
热议问题