import inspect, re
def varname(p):
funcname= 'varname'
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\b%s\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)' %funcname, line)
if m:
return m.group(1)
if __name__ == '__main__':
shit = 233
print varname(shit)
这个可以参考某乎的讨论:
https://www.zhihu.com/question/42768955