3 ms·
Cool. I didn't know that you could get a representation of a function's code through func_name.func_code. That's pretty slick!
by beambot 15y ago
Cool. I didn't know that you could get a representation of a function's code through func_name.func_code. That's pretty slick!
- fpedregosa 15y agoI learned that from Robert Kern's http://packages.python.org/line_profiler/ http://packages.python.org/line_profiler/
- revelation 15y agoYou can not only get it, you can manipulate it at runtime as you wish. I may recommend checking out the python bytecode format, its pretty straightforward. See the module 'dis' for more.
- icebraining 15y agoYeap. I find it great for prototyping: I code in the IPython shell, and then I dump the code of the various functions I wrote to a file, using func_code. def makedumper(filen): def dumpfunc(func): with open(filen, 'a') as f: file.write("\n\n{}".format(func.func_code)) return dumpfunc #then to use it: dump = makedumper("sourcefile") dump(func1) dump(func2)