8

Is there a way to view the source code of a function, class, or module from the python interpreter? (in addition to using help to view the docs and dir to view the attributes/methods)

durron597
  • 31,968
  • 17
  • 99
  • 158
user379260
  • 103
  • 1
  • 5
  • You have the source. What's stopping you from looking at the source? – S.Lott Jun 29 '10 at 21:36
  • 2
    S. Lott: I guess I was looking for something a few steps more convenient than popping up a file explorer/terminal, navigating, and opening up another text editor, particularly when I'm already working with some module – user379260 Jun 30 '10 at 03:21
  • @wkat12: You aren't using an editor? Clearly, I don't understand your use case at all. Please UPDATE your question with additional information on what you're doing, and why you don't have an editor running. – S.Lott Jun 30 '10 at 10:34
  • S.Lott: - ? I use aptana. But it has the directories open for the files that I'm working on, not necessarily what I'm importing. – user379260 Jun 30 '10 at 15:41
  • @wkat12: What? Are you on an system that lacks windows? I really can't follow what you're doing where you can't use an editor or open multiple windows. Please UPDATE the question to describe what you're doing. – S.Lott Jul 01 '10 at 01:49

2 Answers2

19

If you plan to use python interactively it is hard to beat ipython. To print the source of any known function you can then use %psource.

In [1]: import ctypes
In [2]: %psource ctypes.c_bool
class c_bool(_SimpleCData):
_type_ = "?"

The output is even colorized. You can also directly invoke your $EDITOR on the defining source file with %edit.

In [3]: %edit ctypes.c_bool
Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
  • also can use `ctypes.c_bool??` – John La Rooy Jun 29 '10 at 19:24
  • couldn't accept it as the regular answer, but yea, ipython is tempting. – user379260 Jun 29 '10 at 19:43
  • @wkat12, ipython is fantastic, why limit yourself to the shell that comes with Python? – John La Rooy Jun 29 '10 at 19:48
  • @gnibbler: i dunno, i thought that it was attached to the scipy/numpy modules, and those were taking longer for me to understand than I was ready for, but it looks like it's separate (now). also if you're programming code that can be run from the terminal, you'd probably have to watch out for any incompatibilities. might try it again anyhow tho :) – user379260 Jun 30 '10 at 03:28
  • sometimes you have to use a console manhole and can't use ipython ;) – Skylar Saveland Oct 18 '12 at 15:52
  • @SkylarSaveland: Do you mean you run ipython not from the command line? – Benjamin Bannier Oct 18 '12 at 16:51
  • @honk yes, you can `import IPython; IPython.embed()` in the latest versions, which is super-handy to set a break point and jump into the interpreter at that point (I prefer to pdb). Sadly, this is not relevant for twisted. – Skylar Saveland Oct 18 '12 at 17:07
11
>>> import inspect
>>> print(''.join(inspect.getsourcelines(inspect.getsourcelines)[0]))
def getsourcelines(object):
    """Return a list of source lines and starting line number for an object.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of the lines
    corresponding to the object and the line number indicates where in the
    original source file the first line of code was found.  An IOError is
    raised if the source code cannot be retrieved."""
    lines, lnum = findsource(object)

    if ismodule(object): return lines, 0
    else: return getblock(lines[lnum:]), lnum + 1
Duncan
  • 92,073
  • 11
  • 122
  • 156
  • 2
    wow.. too bad we need an import, a print, a join, and an index just to make it readable... still, thanks *edit* i played around with the inspect module after reading this, it looks like you can use "getsource" instead of "getsourcelines" and skip the indexing, i.e. print(''.join(inspect.getsource(obj))) – user379260 Jun 29 '10 at 19:42
  • It doesn't seem to work for built-in classes. – ATL_DEV Dec 19 '22 at 19:57