Created
December 10, 2022 16:00
-
-
Save simryang/c7e864c419ee9592dfbe5effc0cb96bd to your computer and use it in GitHub Desktop.
how to get variable name into string...
This file contains hidden or 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
def retrieve_name(var): | |
callers_local_vars = inspect.currentframe().f_back.f_locals.items() | |
return [var_name for var_name, var_val in callers_local_vars if var_val is var][0] | |
def retrieve_names(var): | |
callers_local_vars = inspect.currentframe().f_back.f_locals.items() | |
return [var_name for var_name, var_val in callers_local_vars if var_val is var] | |
aaa="kkk" | |
bbb="kkk" | |
ccc="kkk" | |
retrieve_name(aaa) | |
# 'aaa' | |
retrieve_names(bbb) | |
# ['aaa', 'bbb', 'ccc'] | |
[var for var, val in locals().items() if val is ccc] | |
# ['aaa', 'bbb', 'ccc'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment