onnes

Furry Physicist and Lead Engineer

  • they/she

Avatar by @stratica
NSFW: @darkonnes


lexi
@lexi

javascript is one of the languages of all time


Osmose
@Osmose

Let me introduce you to the debugging library called q:

import q

foo = 7
q(foo) # Logs foo=7 to /tmp/q

What 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._getframe to get a reference to the stack frame that called q().
  • It then parses the source code of the function that called it with the ast module to figure out whether it's being used as a decorator or function, and to extract the variable names of any arguments being passed.