Getting the name of a variable as a string

前端 未结 23 2651
旧时难觅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:53

    I wrote the package sorcery to do this kind of magic robustly. You can write:

    from sorcery import dict_of
    
    columns = dict_of(n_jobs, users, queues, priorities)
    

    and pass that to the dataframe constructor. It's equivalent to:

    columns = dict(n_jobs=n_jobs, users=users, queues=queues, priorities=priorities)
    

提交回复
热议问题