Getting the name of a variable as a string

前端 未结 23 2664
旧时难觅i
旧时难觅i 2020-11-22 00:19

This thread discusses how to get the name of a function as a string in Python: How to get a function name as a string?

How can I do the same for a variable? As oppose

23条回答
  •  温柔的废话
    2020-11-22 00:38

    I think it's so difficult to do this in Python because of the simple fact that you never will not know the name of the variable you're using. So, in his example, you could do:

    Instead of:

    list_of_dicts = [n_jobs, users, queues, priorities]
    
    dict_of_dicts = {"n_jobs" : n_jobs, "users" : users, "queues" : queues, "priorities" : priorities}
    

提交回复
热议问题