是否可以获取传递给函数的变量的原始变量名? E.g。
foobar = "foo"
def func(var):
print var.origname
那样:
func(foobar)
返回:
>>foobar
修改
我所要做的就是做一个像:
这样的功能def log(soup):
f = open(varname+'.html', 'w')
print >>f, soup.prettify()
f.close()
..并让函数从传递给它的变量名称生成文件名。
我想如果不可能,我每次都必须将变量和变量的名称作为字符串传递。
答案 0 :(得分:48)
编辑:为了说清楚,我不建议使用这个全部,它会破坏,它是一团糟,它无论如何都无法帮助你,但它可以用于娱乐/教育目的。
您可以使用inspect
模块进行攻击,我不建议这样做,但您可以这样做...
import inspect
def foo(a, f, b):
frame = inspect.currentframe()
frame = inspect.getouterframes(frame)[1]
string = inspect.getframeinfo(frame[0]).code_context[0].strip()
args = string[string.find('(') + 1:-1].split(',')
names = []
for i in args:
if i.find('=') != -1:
names.append(i.split('=')[1].strip())
else:
names.append(i)
print names
def main():
e = 1
c = 2
foo(e, 1000, b = c)
main()
输出:
['e', '1000', 'c']
答案 1 :(得分:16)
你做不到。它在传递给函数之前进行了评估。您所能做的就是将其作为字符串传递。
答案 2 :(得分:10)
看起来Ivo打败了我inspect
,但这是另一个实现:
import inspect
def varName(var):
lcls = inspect.stack()[2][0].f_locals
for name in lcls:
if id(var) == id(lcls[name]):
return name
return None
def foo(x=None):
lcl='not me'
return varName(x)
def bar():
lcl = 'hi'
return foo(lcl)
bar()
# 'lcl'
当然,它可以被愚弄:
def baz():
lcl = 'hi'
x='hi'
return foo(lcl)
baz()
# 'x'
道德:不要这样做。
答案 3 :(得分:9)
要添加到Michael Mrozek的答案中,您可以通过以下方式提取确切参数与完整代码:
import re
import traceback
def func(var):
stack = traceback.extract_stack()
filename, lineno, function_name, code = stack[-2]
vars_name = re.compile(r'\((.*?)\).*$').search(code).groups()[0]
print vars_name
return
foobar = "foo"
func(foobar)
# PRINTS: foobar
答案 4 :(得分:5)
如果您知道调用代码的外观,您可以尝试的另一种方法是使用traceback
:
def func(var):
stack = traceback.extract_stack()
filename, lineno, function_name, code = stack[-2]
code
将包含用于调用func
的代码行(在您的示例中,它将是字符串func(foobar)
)。您可以解析它以取出参数
答案 5 :(得分:3)
如果你想要一个Key Value Pair关系,也许使用Dictionary会更好?
...或者如果您尝试从代码中创建一些自动文档,或许像Doxygen(http://www.doxygen.nl/)这样的东西可以为您完成这项工作吗?
答案 6 :(得分:2)
如果您希望在不使用源文件的情况下(例如,从Jupyter Notebook获得),像@Matt Oates answer那样获得调用者参数,则此代码(从@Aeon answer组合)将达到目的(至少在一些简单的情况):
def get_caller_params():
# get the frame object for this function call
thisframe = inspect.currentframe()
# get the parent calling frames details
frames = inspect.getouterframes(thisframe)
# frame 0 is the frame of this function
# frame 1 is the frame of the caller function (the one we want to inspect)
# frame 2 is the frame of the code that calls the caller
caller_function_name = frames[1][3]
code_that_calls_caller = inspect.findsource(frames[2][0])[0]
# parse code to get nodes of abstract syntact tree of the call
nodes = ast.parse(''.join(code_that_calls_caller))
# find the node that calls the function
i_expr = -1
for (i, node) in enumerate(nodes.body):
if _node_is_our_function_call(node, caller_function_name):
i_expr = i
break
# line with the call start
idx_start = nodes.body[i_expr].lineno - 1
# line with the end of the call
if i_expr < len(nodes.body) - 1:
# next expression marks the end of the call
idx_end = nodes.body[i_expr + 1].lineno - 1
else:
# end of the source marks the end of the call
idx_end = len(code_that_calls_caller)
call_lines = code_that_calls_caller[idx_start:idx_end]
str_func_call = ''.join([line.strip() for line in call_lines])
str_call_params = str_func_call[str_func_call.find('(') + 1:-1]
params = [p.strip() for p in str_call_params.split(',')]
return params
def _node_is_our_function_call(node, our_function_name):
node_is_call = hasattr(node, 'value') and isinstance(node.value, ast.Call)
if not node_is_call:
return False
function_name_correct = hasattr(node.value.func, 'id') and node.value.func.id == our_function_name
return function_name_correct
您可以按以下方式运行它:
def test(*par_values):
par_names = get_caller_params()
for name, val in zip(par_names, par_values):
print(name, val)
a = 1
b = 2
string = 'text'
test(a, b,
string
)
获得所需的输出:
a 1
b 2
string text
答案 7 :(得分:2)
我想知道 IceCream 如何解决这个问题。所以我研究了 the source code 并提出了以下(稍微简化的)解决方案。它可能不是 100% 防弹(例如,我删除了 get_text_with_indentation
并且我假设只有一个函数参数),但它适用于不同的测试用例。它本身不需要解析源代码,因此应该比以前的解决方案更健壮和简单。
#!/usr/bin/env python3
import inspect
from executing import Source
def func(var):
callFrame = inspect.currentframe().f_back
callNode = Source.executing(callFrame).node
source = Source.for_frame(callFrame)
expression = source.asttokens().get_text(callNode.args[0])
print(expression, '=', var)
i = 1
f = 2.0
dct = {'key': 'value'}
obj = type('', (), {'value': 42})
func(i)
func(f)
func(s)
func(dct['key'])
func(obj.value)
输出:
i = 1
f = 2.0
s = string
dct['key'] = value
obj.value = 42
更新:如果您想将“魔法”移到一个单独的函数中,您只需再往后移动一帧,并添加一个额外的 f_back
。
def get_name_of_argument():
callFrame = inspect.currentframe().f_back.f_back
callNode = Source.executing(callFrame).node
source = Source.for_frame(callFrame)
return source.asttokens().get_text(callNode.args[0])
def func(var):
print(get_name_of_argument(), '=', var)
答案 8 :(得分:1)
@Ivo Wetzel的答案在函数调用的情况下可以在一行中完成,例如
e = 1 + 7
c = 3
foo(e, 100, b=c)
如果函数调用不在一行中,例如:
e = 1 + 7
c = 3
foo(e,
1000,
b = c)
以下代码有效:
import inspect, ast
def foo(a, f, b):
frame = inspect.currentframe()
frame = inspect.getouterframes(frame)[1]
string = inspect.findsource(frame[0])[0]
nodes = ast.parse(''.join(string))
i_expr = -1
for (i, node) in enumerate(nodes.body):
if hasattr(node, 'value') and isinstance(node.value, ast.Call)
and hasattr(node.value.func, 'id') and node.value.func.id == 'foo' # Here goes name of the function:
i_expr = i
break
i_expr_next = min(i_expr + 1, len(nodes.body)-1)
lineno_start = nodes.body[i_expr].lineno
lineno_end = nodes.body[i_expr_next].lineno if i_expr_next != i_expr else len(string)
str_func_call = ''.join([i.strip() for i in string[lineno_start - 1: lineno_end]])
params = str_func_call[str_func_call.find('(') + 1:-1].split(',')
print(params)
你会得到:
[u'e', u'1000', u'b = c']
但是,这可能会破裂。
答案 9 :(得分:0)
由于可以有多个具有相同内容的变量,而不是传递变量(内容),所以将其名称传递到字符串中并从locals字典中获取变量内容可能更安全(并且将更简单)。调用者堆栈框架。 :
def displayvar(name):
import sys
return name+" = "+repr(sys._getframe(1).f_locals[name])
答案 10 :(得分:0)
为了繁荣,这里有一些我为执行此任务而编写的代码,总的来说,我认为Python中缺少一个模块,可以为所有人提供对调用者环境的良好而强大的检查。类似于Rlang eval框架在R中提供的功能。
import re, inspect, ast
#Convoluted frame stack walk and source scrape to get what the calling statement to a function looked like.
#Specifically return the name of the variable passed as parameter found at position pos in the parameter list.
def _caller_param_name(pos):
#The parameter name to return
param = None
#Get the frame object for this function call
thisframe = inspect.currentframe()
try:
#Get the parent calling frames details
frames = inspect.getouterframes(thisframe)
#Function this function was just called from that we wish to find the calling parameter name for
function = frames[1][3]
#Get all the details of where the calling statement was
frame,filename,line_number,function_name,source,source_index = frames[2]
#Read in the source file in the parent calling frame upto where the call was made
with open(filename) as source_file:
head=[source_file.next() for x in xrange(line_number)]
source_file.close()
#Build all lines of the calling statement, this deals with when a function is called with parameters listed on each line
lines = []
#Compile a regex for matching the start of the function being called
regex = re.compile(r'\.?\s*%s\s*\(' % (function))
#Work backwards from the parent calling frame line number until we see the start of the calling statement (usually the same line!!!)
for line in reversed(head):
lines.append(line.strip())
if re.search(regex, line):
break
#Put the lines we have groked back into sourcefile order rather than reverse order
lines.reverse()
#Join all the lines that were part of the calling statement
call = "".join(lines)
#Grab the parameter list from the calling statement for the function we were called from
match = re.search('\.?\s*%s\s*\((.*)\)' % (function), call)
paramlist = match.group(1)
#If the function was called with no parameters raise an exception
if paramlist == "":
raise LookupError("Function called with no parameters.")
#Use the Python abstract syntax tree parser to create a parsed form of the function parameter list 'Name' nodes are variable names
parameter = ast.parse(paramlist).body[0].value
#If there were multiple parameters get the positional requested
if type(parameter).__name__ == 'Tuple':
#If we asked for a parameter outside of what was passed complain
if pos >= len(parameter.elts):
raise LookupError("The function call did not have a parameter at postion %s" % pos)
parameter = parameter.elts[pos]
#If there was only a single parameter and another was requested raise an exception
elif pos != 0:
raise LookupError("There was only a single calling parameter found. Parameter indices start at 0.")
#If the parameter was the name of a variable we can use it otherwise pass back None
if type(parameter).__name__ == 'Name':
param = parameter.id
finally:
#Remove the frame reference to prevent cyclic references screwing the garbage collector
del thisframe
#Return the parameter name we found
return param
答案 11 :(得分:0)
你可以使用 python-varname 包
from varname import nameof
s = 'Hey!'
print (nameof(s))
输出:
s
包装如下: