Let me introduce you to the debugging library called q:
import q
foo = 7
q(foo) # Logs foo=7 to /tmp/qWhat is the type of q? If you guessed "callable module", you'd be correct.
Callable modules are not a thing in Python.
By the way, you can also do q/expr or q|expr to log expr. You can also decorate a function with @q to log it's arguments, runtime, and return value. Also, q is able to log the name of the variable you pass as an argument.
I wrote a post a while ago detailing exactly how it works, but the tl;dr is:
- q modifies the value in
sys.modules['q']to make the q module a value that isn't a module. - It uses
sys._getframeto get a reference to the stack frame that calledq(). - It then parses the source code of the function that called it with the
astmodule to figure out whether it's being used as a decorator or function, and to extract the variable names of any arguments being passed.