Is there a way to display source code for Python function?

Viet Nguyen vhnguyenn at yahoo.com
Fri Oct 3 02:13:12 EDT 2014


On Thursday, October 2, 2014 10:47:28 PM UTC-7, Ian wrote:
> On Thu, Oct 2, 2014 at 11:34 PM, Viet Nguyen
> 
> <vhnguyenn at yahoo.com.dmarc.invalid> wrote:
> 
> > Hi,
> 
> >
> 
> > When I am debug mode, is there some command which will help display the source code for a Python function of interest?  Much like you'd use "info proc" to display contents of Tcl proc.
> 
> >
> 
> > Thanks,
> 
> > Viet
> 
> 
> 
> You can use inspect.getsource() to get the source code for a function,
> 
> class, or module. The source must be available at whatever location
> 
> the module was imported from, and of course it won't work for anything
> 
> implemented in C.

Hi,
I tried:

def func(a):
  a = 'abcd'

>>> inspect.getsource(func)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/sw/packages/python/current/lib/python2.7/inspect.py", line 701, in getsource
    lines, lnum = getsourcelines(object)
  File "/sw/packages/python/current/lib/python2.7/inspect.py", line 690, in getsourcelines
    lines, lnum = findsource(object)
  File "/sw/packages/python/current/lib/python2.7/inspect.py", line 538, in findsource
    raise IOError('could not get source code')
IOError: could not get source code


What is wrong?

Thanks,
Viet




More information about the Python-list mailing list