After reading about Python inspect, I realized that a lot of information is available to a function being called. One can even look downwards into the scope of the caller. It occurred to me that this could be useful for debugging -- as a companion to the good-old "print values to stdout and see what's wrong" technique. Here is what I call Printval -- it uses introspection to automagically print both the expression and the value. It uses the | operator for quick typing.
The entire code to make printval work (including expanding structures and classes) is only a few lines long:
Helpfully, it will enumerate through the fields of a class. And if you use printval| locals(), you get a more-nicely formatted representation of all locals in scope.
It's such a hack to be able to introspect *into the source code*. (In an earlier version, I used the syntax printval.a, so I could create chains like printval.a.b. This method doesn't require looking at source code -- the printval object knows the string 'a' from the parameter passed to __getattribute__, and retrieves its value from the scope of the caller. As a benefit this could work in compiled/frozen apps. The downside is that this doesn't allow expressions like printval|a+4)
I've since learned that other people have had similar ideas; there are modules for ScopeFormatter, Say, and Show.
I've added the source to GitHub here under the GPLv3.