Created
January 5, 2022 03:36
-
-
Save shininglion/32335c98f99116c4864b2ec4beaa97af to your computer and use it in GitHub Desktop.
Use string to access python variable
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
def print_local(var_str): | |
def local_def(): | |
print(local_def) | |
local_local = 2 | |
symtbl = locals() | |
print(symtbl) | |
if var_str in symtbl: | |
print(symtbl[var_str]) | |
else: | |
print(f"Failed to find symbol '{var_str}'") | |
def print_global(var_str): | |
local_global = 3 | |
symtbl = globals() | |
print(symtbl) | |
if var_str in symtbl: | |
print(symtbl[var_str]) | |
else: | |
print(f"Failed to find symbol '{var_str}'") | |
for line in sys.stdin: | |
str = line.strip() | |
print_local(str) | |
print_global(str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment