TL;DR:不可能。见最后的“结论”。
有一个使用场景,您可能需要它。我并不是暗示没有更好的方法或实现相同的功能。
这对于在出错、调试模式和其他类似情况下“转储”任意字典列表很有用。
需要的是 eval() 函数的反函数:
get_indentifier_name_missing_function()
将标识符名称('变量','字典'等)作为参数,并返回一个
包含标识符名称的字符串。
考虑以下现状:
random_function(argument_data)
如果将标识符名称('function'、'variable'、'dictionary'等)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' 来:
-
用作“标识符名称”(用于显示、记录、字符串拆分/合并等)
-
输入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' 字符串解析为实际标识符时才有效。 IE。如果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
现在,考虑 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 标识符名称。
因此,真正的结果宁愿是:
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) )
######
总结:
- Python 仅将内存地址作为参数传递给函数。
- 如果名称标识符在当前命名空间中可用,则表示标识符名称的字符串只能通过
eval() 函数引用回实际标识符。
-
eval() 函数的假设反转在调用代码未直接“看到”标识符名称的情况下将没有用。例如。在任何被调用的函数中。
- 目前需要传递给一个函数:
- 代表标识符名称的字符串
- 实际标识符(内存地址)
这可以通过同时将'string' 和eval('string') 传递给被调用函数来实现。我认为这是跨任意函数、模块、名称空间解决这个蛋鸡问题的最“通用”方法,而不使用极端情况解决方案。唯一的缺点是使用了eval() 函数,这很容易导致代码不安全。必须注意不要向eval() 函数提供任何东西,尤其是未经过滤的外部输入数据。