Created
March 3, 2025 14:36
-
-
Save ncalm/5971774249e5251bd19d5145cf20f2e8 to your computer and use it in GitHub Desktop.
Print the variables in your global scope in Python in Excel
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
import inspect | |
begin_print = False | |
module_print = True | |
function_print = True | |
for key, value in list(globals().items()): | |
if begin_print and not key.endswith("_print"): | |
# print imported module names if requested | |
if module_print and inspect.ismodule(value): | |
print(f"module {key}") | |
# print function definitions if requested | |
elif callable(value) and function_print: | |
print(f"{inspect.getsource(value).strip()}") | |
# print other variables | |
elif not inspect.ismodule(value) and not callable(value): | |
print(f"{type(value).__name__} {key}={value}") | |
# user variables appear after xl() in the global scope | |
elif key == "xl" and callable(value): | |
begin_print = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment