For example, I want to check the source code of a python library, directly in the notebook, is there a way to do that?
Thank you
We use the getsource() method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.
getsource() method is used to get the source code of Python objects. An IOError is raised if the source code cannot be retrieved. Example: We can use inspect on built in library functions and objects also.
To run a piece of code, click on the cell to select it, then press SHIFT+ENTER or press the play button in the toolbar above. Additionally, the Cell dropdown menu has several options to run cells, including running one cell at a time or to run all cells at once.
Open the jupyter notebook that you want to convert. Navigate into the 'File' menu and select 'Download as'. The more options will be displayed in the form of a list where you will click on the 'Python (. py)' option.
Type the fully qualified function name, then type ??
.
You can use inspect module (which is built in) - for example you can see the tree module below from scikit-learn.
import inspect from sklearn import tree inspect.getsourcelines(tree.tree)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With