获取字符串形式的参数名称 - Python
Getting an argument name as a string - Python
我有一个函数接受一个变量作为参数。该变量恰好包含一个目录,还包含一堆txt文件。
我想知道是否有办法将该变量视为字符串?不是变量内部的内容,只是该变量的名称。
非常感谢!
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")
虽然这是一个非常奇怪的请求,但这是从函数内部获取参数名称的方法
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
我有一个函数接受一个变量作为参数。该变量恰好包含一个目录,还包含一堆txt文件。
我想知道是否有办法将该变量视为字符串?不是变量内部的内容,只是该变量的名称。 非常感谢!
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")
虽然这是一个非常奇怪的请求,但这是从函数内部获取参数名称的方法
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