Created
March 31, 2020 08:25
-
-
Save ayuLiao/f0b1ffcd530670788dcc5665012ab66c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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