2016-08-23 35 views
1

我有一个函数,它接受一个变量作为参数。该变量恰好包含一个目录,也包含一堆txt文件。获取参数名称作为字符串 - Python

我想知道是否有一种方法将该变量视为一个字符串?不是变量内部的内容,只是该变量的名称。 非常感谢!

import glob 
import pandas as pd 

variable_1 = glob.glob('dir_pathway/*txt') 
variable_2 = glob.glob('other_dir_pathway/*txt') 

def my_function(variable_arg): 
    ## a bunch of code to get certain things from the directory ## 
    variable_3 = pd.DataFrame(## stuff taken from directory ##) 
    variable_3.to_csv(variable_arg + "_add_var_to_me.txt") 
+0

所以你基本上要的功能'name_of_var'所以'a = 1; name_of_var(a)'返回'a'? –

+0

http://stackoverflow.com/a/1373185/918959? –

回答

2

虽然这是非常奇怪的要求,这里是你如何在函数内部

import inspect 

def f(value): 
    frame = inspect.currentframe() 
    print(inspect.getargvalues(frame)[0][0]) 

f(10) 
f("Hello world") 
f([1, 2, 3]) 

打印件的参数的名称

value 
value 
value 
相关问题