Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created March 31, 2020 08:25
Show Gist options
  • Save ayuLiao/f0b1ffcd530670788dcc5665012ab66c to your computer and use it in GitHub Desktop.
Save ayuLiao/f0b1ffcd530670788dcc5665012ab66c to your computer and use it in GitHub Desktop.
import inspect, re
def varname(p):
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
m = re.search(r'\bvarname\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
if m:
return m.group(1)
if __name__ == '__main__':
spam = 42
print varname(spam)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment