Python : pass variable name as argument

后端 未结 4 1270
渐次进展
渐次进展 2021-01-16 12:22

I have a function f(x) in which many local variables are created. x is a string with the same name as one of these local variables and I would like

4条回答
  •  一向
    一向 (楼主)
    2021-01-16 12:55

    If your input is a string and you want to refer to a variable that matches that string you can use globals() like this:

    globals()['x']
    

    This way you can get the value and/or edit its contents.

提交回复
热议问题