Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to view the source of a module from within the python console? [duplicate]

Tags:

if I'm in the python console and I want to see how a particular module works, is there an easy way to dump the source?

like image 469
tehryan Avatar asked Jan 13 '10 07:01

tehryan


2 Answers

Some of the methods of inspect module are well-suited for this purpose:

import module import inspect src = inspect.getsource(module) 
like image 200
Alok Singhal Avatar answered Oct 20 '22 20:10

Alok Singhal


Using IPython, you can do this:

In [1]: import pyparsing In [2]: pyparsing.Word?? 

...and the source will be displayed, if possible. I guess this must use inspect under the hood, but the added convenience of ? / ?? is fantastic.

like image 34
Michał Marczyk Avatar answered Oct 20 '22 20:10

Michał Marczyk