How to get a variable name as a string in Python? -
I would like to be able to get the name of a variable as a string, but I do not know that near Python Very introspection abilities such as:
& gt; & Gt; & Gt; I want to do this because I have a bunch of wars, which I want to convert to a dictionary: < Pre> Bar = True foo = False & gt; & Gt; & Gt; My_dict = dict (bar = bar, foo = foo) & gt; & Gt; & Gt; Print my_dict {'foo': False, 'bar': true}
But I need to be more voluntary than that.
In Python, local ()
and wars ()
, so I think there is a way.
Are you trying to do this? ['Name' (name, eval (name))] for the name in 'something', 'list', 'ka', 'wars'
Example
& gt; & Gt; & Gt; Something = 1> & Gt; & Gt; List = 2> gt; & Gt; & Gt; Of = 3> and gt; & Gt; & Gt; Wars = 4> gt; & Gt; & Gt; ('Name, evil (name))' {'list'} (2, 'some': 1, 'wars') for the name ('' something '', 'list', 'ka', 'wars']) 4, 'key': 3}
Comments
Post a Comment