如何获取传递给函数的变量的原始变量名 [英] How to get the original variable name of variable passed to a function
本文介绍了如何获取传递给函数的变量的原始变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以获取传递给函数的变量的原始变量名?例如
Is it possible to get the original variable name of a variable passed to a function? E.g.
foobar = "foo"
def func(var):
print var.origname
所以:
func(foobar)
返回:
>>foobar
我想做的就是做一个像这样的函数:
All I was trying to do was make a function like:
def log(soup):
f = open(varname+'.html', 'w')
print >>f, soup.prettify()
f.close()
.. 并让函数根据传递给它的变量的名称生成文件名.
.. and have the function generate the filename from the name of the variable passed to it.
我想如果不可能的话,我只需要每次都将变量和变量的名称作为字符串传递.
I suppose if it's not possible I'll just have to pass the variable and the variable's name as a string each time.
推荐答案
你不能.它在传递给函数之前被评估.您所能做的就是将其作为字符串传递.
You can't. It's evaluated before being passed to the function. All you can do is pass it as a string.
这篇关于如何获取传递给函数的变量的原始变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文