python将变量转换为字符串_将变量名转换为字符串?

有一个使用场景,您可能需要它。我并不是说没有更好的方法或者实现相同的功能。

这对于在出现错误、调试模式和其他类似情况下“转储”任意字典列表非常有用。

我们需要的是eval()函数的反面:get_indentifier_name_missing_function()

将标识符名称('variable','dictionary',等)作为参数,并返回

包含标识符名称的字符串。

考虑以下现状:random_function(argument_data)

如果将标识符名称('function','variable','dictionary',etc)argument_data传递给random_function()(另一个标识符名称),则实际上将标识符(例如:)传递给另一个标识符(例如:):()

据我所知,只有内存地址被传递给函数:()

因此,需要将字符串作为参数传递给random_function(),以便该函数具有参数的标识符名称:random_function('argument_data')

在随机函数中()def random_function(first_argument):

,将使用已提供的字符串'argument_data'来:充当“标识符名称”(用于显示、记录、字符串拆分/concat等)

馈送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'字符串解析为实际标识符时,它才起作用。一、 e.如果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

some_module1.random_function('argument_data')

File "~/some_module1.py", line 4, in random_function

some_internal_var = eval(first_argument)

File "", line 1, in

NameError: name 'argument_data' is not defined

现在,考虑一下get_indentifier_name_missing_function()的低血压用法,它的行为如上所述。

这是一个虚拟的Python3.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()函数的反面在调用代码不能直接“看到”标识符名称的情况下是不有用的。E、 在任何被调用的函数中。

当前需要传递给函数:

表示标识符名称的字符串

实际标识符(内存地址)

这可以通过同时将'string'和eval('string')传递给被调用函数来实现。我认为这是解决这个蛋鸡问题的最“通用”的方法,它可以跨越任意函数、模块、名称空间,而无需使用角大小写解决方案。唯一的缺点是使用eval()函数,这很容易导致不安全的代码。必须注意不要给eval()函数提供任何东西,特别是未过滤的外部输入数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值