Python's argparse: How to use keyword as argument's name

后端 未结 3 1920
鱼传尺愫
鱼传尺愫 2021-01-18 00:30

lambda has a keyword function in Python:

f = lambda x: x**2 + 2*x - 5

What if I want to use it as a variable name? Is there an

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 00:43

    There is an argparse-specific way of dealing with this. From the documentation:

    If you prefer to have dict-like view of the attributes, you can use the standard Python idiom, vars().

    Therefore, you should be able to write:

    print vars(args)["lambda"]  # No keyword used, no syntax error.
    

提交回复
热议问题