How to use reserved keyword as the name of variable in python?

后端 未结 1 1409
别跟我提以往
别跟我提以往 2021-01-21 05:57

I want to use reserved keyword \"from\" as the name of variable.

I have it in my arguments parser:

parser.add_argument(\"--from\")
args = parser.parse_ar         


        
相关标签:
1条回答
  • 2021-01-21 06:50

    You can use getattr() to access the attribute:

    print(getattr(args, 'from'))
    

    However, in argparse you can have the command-line option --from without having to have the attribute from by using the dest option to specify an alternative name to use:

    parser.add_argument('--from', dest='from_')
    
    # ...
    args = parser.parse_args()
    print(args.from_)
    
    0 讨论(0)
提交回复
热议问题