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_else = 3
print ??? # Would print 'something_else'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
TL;DR: Not possible. See ‘conclusion’ at the end.
There is an usage scenario where you might need this. I’m not implying there are not better ways or achieving the same functionality.
This would be useful in order to ‘dump’ an arbitrary list of dictionaries in case of error, in debug modes and other similar situations.
What would be needed, is the reverse of the
eval()function:which would take an identifier name (‘variable’,’dictionary’,etc) as an argument, and return a
string containing the identifier’s name.
Consider the following current state of affairs:
If one is passing an identifier name (‘function’,’variable’,’dictionary’,etc)
argument_datato arandom_function()(another identifier name), one actually passes an identifier (e.g.:<argument_data object at 0xb1ce10>) to another identifier (e.g.:<function random_function at 0xafff78>):From my understanding, only the memory address is passed to the function:
Therefore, one would need to pass a string as an argument to
random_function()in order for that function to have the argument’s identifier name:Inside the random_function()
, one would use the already supplied string
'argument_data'to:serve as an ‘identifier name’ (to display, log, string split/concat, whatever)
feed the
eval()function in order to get a reference to the actual identifier, and therefore, a reference to the real data:Unfortunately, this doesn’t work in all cases. It only works if the
random_function()can resolve the'argument_data'string to an actual identifier. I.e. Ifargument_dataidentifier name is available in therandom_function()‘s namespace.This isn’t always the case:
Expected results would be:
Because
argument_dataidentifier name is not available in therandom_function()‘s namespace, this would yield instead:Now, consider the hypotetical usage of a
get_indentifier_name_missing_function()which would behave as described above.Here’s a dummy Python 3.0 code: .
Expected results would be:
Unfortunately,
get_indentifier_name_missing_function()would not see the ‘original’ identifier names (some_dictionary_,some_other_dictionary_2,some_other_dictionary_n). It would only see thea_dictionary_objectidentifier name.Therefore the real result would rather be:
So, the reverse of the
eval()function won’t be that useful in this case.Currently, one would need to do this:
In conclusion:
eval()function if the name identifier is available in the current namespace.eval()function, would not be useful in cases where the identifier name is not ‘seen’ directly by the calling code. E.g. inside any called function.This can be achieved by passing both the
'string'andeval('string')to the called function at the same time. I think this is the most ‘general’ way of solving this egg-chicken problem across arbitrary functions, modules, namespaces, without using corner-case solutions. The only downside is the use of theeval()function which may easily lead to unsecured code. Care must be taken to not feed theeval()function with just about anything, especially unfiltered external-input data.