如何获取传递给函数的变量的原始变量名称 [英] How to get the original variable name of variable passed to a function

查看:117
本文介绍了如何获取传递给函数的变量的原始变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得传递给函数的变量的原始变量名?例如

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

因此:

So that:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆