Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78636 > unrolled thread

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

Started byViet Nguyen <vhnguyenn@yahoo.com>
First post2014-10-02 22:34 -0700
Last post2014-10-03 13:04 +0100
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Is there a way to display source code for Python function? Viet Nguyen <vhnguyenn@yahoo.com> - 2014-10-02 22:34 -0700
    Re: Is there a way to display source code for Python function? Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-02 23:46 -0600
      Re: Is there a way to display source code for Python function? Viet Nguyen <vhnguyenn@yahoo.com> - 2014-10-02 23:13 -0700
    Re: Is there a way to display source code for Python function? Viet Nguyen <vhnguyenn@yahoo.com> - 2014-10-02 23:09 -0700
      Re: Is there a way to display source code for Python function? Peter Otten <__peter__@web.de> - 2014-10-03 09:47 +0200
        Re: Is there a way to display source code for Python function? Viet Nguyen <vhnguyenn@yahoo.com> - 2014-10-03 01:16 -0700
          Re: Is there a way to display source code for Python function? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-03 13:04 +0100

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

FromViet Nguyen <vhnguyenn@yahoo.com>
Date2014-10-02 22:34 -0700
SubjectIs there a way to display source code for Python function?
Message-ID<2d5b0e56-992a-481c-9a8b-eb546cd9ac2e@googlegroups.com>
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

[toc] | [next] | [standalone]


#78637

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-02 23:46 -0600
Message-ID<mailman.14570.1412315234.18130.python-list@python.org>
In reply to#78636
On Thu, Oct 2, 2014 at 11:34 PM, Viet Nguyen
<vhnguyenn@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.

[toc] | [prev] | [next] | [standalone]


#78639

FromViet Nguyen <vhnguyenn@yahoo.com>
Date2014-10-02 23:13 -0700
Message-ID<5cb0f9ec-b0b2-4350-a666-27f484af4720@googlegroups.com>
In reply to#78637
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@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

[toc] | [prev] | [next] | [standalone]


#78638

FromViet Nguyen <vhnguyenn@yahoo.com>
Date2014-10-02 23:09 -0700
Message-ID<fb1478cc-e18f-4fd3-a59e-38b7538948a6@googlegroups.com>
In reply to#78636
On Thursday, October 2, 2014 10:34:15 PM UTC-7, Viet Nguyen 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

I tried this:
>>> 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

>>> inspect.getsourcelines(func)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  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

[toc] | [prev] | [next] | [standalone]


#78641

FromPeter Otten <__peter__@web.de>
Date2014-10-03 09:47 +0200
Message-ID<mailman.14571.1412322468.18130.python-list@python.org>
In reply to#78638
Viet Nguyen wrote:

> On Thursday, October 2, 2014 10:34:15 PM UTC-7, Viet Nguyen 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
> 
> I tried this:
>>>> 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
> 
>>>> inspect.getsourcelines(func)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   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?

The source of func is compiled and immediately discarded by the interactive 
interpreter. inspect.getsource() only works if the source code is available 
(as a module):

$ cat ham.py
def spam():
    return 42
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ham, inspect
>>> print(inspect.getsource(ham.spam))
def spam():
    return 42

>>> 

There is an alternative interactive interpreter called ipython that allows 
you to retrieve a function definition:

In [1]: def foo():
   ...:     return bar
   ...: 

In [2]: %psource foo
def foo():
    return bar

In [3]: 


[toc] | [prev] | [next] | [standalone]


#78644

FromViet Nguyen <vhnguyenn@yahoo.com>
Date2014-10-03 01:16 -0700
Message-ID<644f93f2-38c8-4d40-b49f-587150b0ff33@googlegroups.com>
In reply to#78641
On Friday, October 3, 2014 12:48:08 AM UTC-7, Peter Otten wrote:
> Viet Nguyen wrote:
> 
> 
> 
> > On Thursday, October 2, 2014 10:34:15 PM UTC-7, Viet Nguyen 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
> 
> > 
> 
> > I tried this:
> 
> >>>> 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
> 
> > 
> 
> >>>> inspect.getsourcelines(func)
> 
> > Traceback (most recent call last):
> 
> >   File "<stdin>", line 1, in <module>
> 
> >   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?
> 
> 
> 
> The source of func is compiled and immediately discarded by the interactive 
> 
> interpreter. inspect.getsource() only works if the source code is available 
> 
> (as a module):
> 
> 
> 
> $ cat ham.py
> 
> def spam():
> 
>     return 42
> 
> $ python3
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
> 
> [GCC 4.8.2] on linux
> 
> Type "help", "copyright", "credits" or "license" for more information.
> 
> >>> import ham, inspect
> 
> >>> print(inspect.getsource(ham.spam))
> 
> def spam():
> 
>     return 42
> 
> 
> 
> >>> 
> 
> 
> 
> There is an alternative interactive interpreter called ipython that allows 
> 
> you to retrieve a function definition:
> 
> 
> 
> In [1]: def foo():
> 
>    ...:     return bar
> 
>    ...: 
> 
> 
> 
> In [2]: %psource foo
> 
> def foo():
> 
>     return bar
> 
> 
> 
> In [3]:

Thanks Peter!

[toc] | [prev] | [next] | [standalone]


#78648

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-03 13:04 +0100
Message-ID<mailman.14576.1412337918.18130.python-list@python.org>
In reply to#78644
On 03/10/2014 09:16, Viet Nguyen wrote:

[snip normal google stuff]

Would you please access this list via 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web