问题
I've got a system in which I constantly experiment with the contents of a function, after which I run the program. Since I often have many terminal windows open, I sometimes don't exactly know which version of the function belongs to which terminal window.
Is there a way to print the source code of a specific function to the terminal?
回答1:
Alright. Thanks to Hoopdady who provided a similar question here on Stackoverflow. Just to give the solution for future readers. I needed to use the inspect module, and using its method "getsource" I can simply print the source of a method or function to the command line.
import inspect
import mymodule
print inspect.getsource(mymodule.sayHello)
Thanks Hoopdady!
来源:https://stackoverflow.com/questions/15297413/how-to-print-the-python-code-of-a-function-to-the-terminal