我想将python变量名称转换为等效的字符串,如图所示。有什么想法吗?
var = {} print ??? # Would like to see 'var' something_else = 3 print ??? # Would print 'something_else'
在一个使用场景中,您可能需要此场景。我并不是在暗示没有更好的方法或实现相同的功能。
为了在调试,模式和其他类似情况下出现错误时“转储”任意词典列表,这将很有用。
需要的是该eval()功能的反向功能:
eval()
get_indentifier_name_missing_function()
它将以标识符名称(“变量”,“字典”等)作为参数,并返回包含标识符名称的字符串。
考虑以下当前状态:
random_function(argument_data)
如果将一个标识符名称(“函数”,“变量”,“字典”等)argument_data传递给random_function()(另一个标识符名称),则实际上会将一个标识符(例如:)传递<argument_data object at 0xb1ce10>给另一个标识符(例如:)<function random_function at 0xafff78>:
argument_data
random_function()
<argument_data object at 0xb1ce10>
<function random_function at 0xafff78>
<function random_function at 0xafff78>(<argument_data object at 0xb1ce10>)
据我了解,只有内存地址才传递给函数:
<function at 0xafff78>(<object at 0xb1ce10>)
因此,random_function()为了使该函数具有参数的标识符名称,需要将字符串作为参数传递给:
random_function('argument_data')
内部random_function()
def random_function(first_argument):
,则可以使用已经提供的字符串'argument_data'执行以下操作:
'argument_data'
用作“标识符名称”(以显示,记录,字符串拆分/合并等)
输入eval()函数以获取对实际标识符的引用,从而获得对实际数据的引用:
print("Currently working on", first_argument)
some_internal_var = eval(first_argument) print(“here comes the data: ” + str(some_internal_var))
不幸的是,这并非在所有情况下都有效。仅当random_function()可以将'argument_data'字符串解析为实际标识符时,它才有效。即,如果argument_data标识符名称在random_function()的命名空间中可用。
并非总是这样:
# main1.py import some_module1 argument_data = 'my data' some_module1.random_function('argument_data') # some_module1.py def random_function(first_argument): print("Currently working on", first_argument) some_internal_var = eval(first_argument) print("here comes the data: " + str(some_internal_var)) ######
预期结果将是:
Currently working on: argument_data here comes the data: my data
由于argument_data标识符名称在random_function()的命名空间中不可用,因此将产生:
Currently working on argument_data Traceback (most recent call last): File "~/main1.py", line 6, in <module> some_module1.random_function('argument_data') File "~/some_module1.py", line 4, in random_function some_internal_var = eval(first_argument) File "<string>", line 1, in <module> NameError: name 'argument_data' is not defined
现在,考虑a的假设用法,get_indentifier_name_missing_function()其行为如上所述。
这是一个虚拟的Python 3.0代码:。
# main2.py import some_module2 some_dictionary_1 = { 'definition_1':'text_1', 'definition_2':'text_2', 'etc':'etc.' } some_other_dictionary_2 = { 'key_3':'value_3', 'key_4':'value_4', 'etc':'etc.' } # # more such stuff # some_other_dictionary_n = { 'random_n':'random_n', 'etc':'etc.' } for each_one_of_my_dictionaries in ( some_dictionary_1, some_other_dictionary_2, ..., some_other_dictionary_n ): some_module2.some_function(each_one_of_my_dictionaries) # some_module2.py def some_function(a_dictionary_object): for _key, _value in a_dictionary_object.items(): print( get_indentifier_name_missing_function(a_dictionary_object) + " " + str(_key) + " = " + str(_value) ) ######
some_dictionary_1 definition_1 = text_1 some_dictionary_1 definition_2 = text_2 some_dictionary_1 etc = etc. some_other_dictionary_2 key_3 = value_3 some_other_dictionary_2 key_4 = value_4 some_other_dictionary_2 etc = etc. ...... ...... ...... some_other_dictionary_n random_n = random_n some_other_dictionary_n etc = etc.
不幸的是,get_indentifier_name_missing_function()不会看到“原始的”标识符名称(some_dictionary_,some_other_dictionary_2,some_other_dictionary_n)。它只会看到a_dictionary_object标识符名称。
some_dictionary_
some_other_dictionary_2
some_other_dictionary_n
a_dictionary_object
因此,实际结果将是:
a_dictionary_object definition_1 = text_1 a_dictionary_object definition_2 = text_2 a_dictionary_object etc = etc. a_dictionary_object key_3 = value_3 a_dictionary_object key_4 = value_4 a_dictionary_object etc = etc. ...... ...... ...... a_dictionary_object random_n = random_n a_dictionary_object etc = etc.
因此,eval()在这种情况下,函数的反向操作不会那么有用。
当前,需要这样做:
# main2.py same as above, except: for each_one_of_my_dictionaries_names in ( 'some_dictionary_1', 'some_other_dictionary_2', '...', 'some_other_dictionary_n' ): some_module2.some_function( { each_one_of_my_dictionaries_names : eval(each_one_of_my_dictionaries_names) } ) # some_module2.py def some_function(a_dictionary_name_object_container): for _dictionary_name, _dictionary_object in a_dictionary_name_object_container.items(): for _key, _value in _dictionary_object.items(): print( str(_dictionary_name) + " " + str(_key) + " = " + str(_value) ) ######
这可以通过同时将'string'和传递eval('string')给被调用的函数来实现。我认为这是跨任意函数,模块,名称空间解决此蛋鸡问题的最“通用”方法,而无需使用极端案例解决方案。唯一的缺点是eval()函数的使用,这很容易导致代码不安全。必须注意不要向eval()函数提供几乎任何东西,尤其是未经过滤的外部输入数据。
'string'
eval('string')