Convert Variable Name to String?

前端 未结 16 1669
生来不讨喜
生来不讨喜 2020-11-28 04:33

I would like to convert a python variable name into the string equivalent as shown. Any ideas how?

var = {}
print ???  # Would like to see \'var\'
something_         


        
16条回答
  •  误落风尘
    2020-11-28 04:56

    You somehow have to refer to the variable you want to print the name of. So it would look like:

    print varname(something_else)
    

    There is no such function, but if there were it would be kind of pointless. You have to type out something_else, so you can as well just type quotes to the left and right of it to print the name as a string:

    print "something_else"
    

提交回复
热议问题