How to do monkeypatching in python?

I needed to do some introspection in python, and that was ugly:

name = sys._getframe(1).f_code name = "%s:%d %s()" %(os.path.split(name.co_filename)[1],name.co_firstlineno,name.co_name) 

To get something like

 foo.py:22 bar() blah blah 

In our debug output.

Ideally, I would like to add something to stderr with such information - Is it possible to change print behavior around the world within python?

+7
python monkeypatching
source share
2 answers

The print statement does its IO through "sys.stdout.write", so you can override sys.stdout if you want to control the print flow. A.

+3
source share

The python inspect module makes this a lot easier and cleaner.

+1
source share

All Articles