Using the python-varname
package, you can easily retrieve the name of the variables
https://github.com/pwwang/python-varname
As of v0.6.0
, in your case, you can do:
from varname.helpers import Wrapper
foo = Wrapper(dict())
# foo.name == 'foo'
# foo.value == {}
foo.value['bar'] = 2
For list comprehension part, you can do:
n_jobs = Wrapper(<original_value>)
users = Wrapper(<original_value>)
queues = Wrapper(<original_value>)
priorities = Wrapper(<original_value>)
list_of_dicts = [n_jobs, users, queues, priorities]
columns = [d.name for d in list_of_dicts]
# ['n_jobs', 'users', 'queues', 'priorities']
# REMEMBER that you have to access the <original_value> by d.value
Update: 07/08/2021
To get the name of a data frame, you can write your own wrapper using argname2
(as of v0.7.1):
>>> # argname is superseded by argname2
>>> from varname import argname2
>>> from pandas import DataFrame
>>>
>>> df1 = DataFrame(dict(a=[1]))
>>> df2 = DataFrame(dict(b=[1]))
>>>
>>> def handle_dfs(*dfs):
... names = argname2('*dfs')
... for name, df in zip(names, dfs):
... df.__dfname__ = name
...
... for df in dfs:
... print(f'name: {df.__dfname__}, value: {df}')
...
>>> handle_dfs(df1, df2)
name: df1, value: a
0 1
name: df2, value: b
0 1
I am the author of this package. Please let me know if you have any questions or you can submit issues on Github.