Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78641
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'subject:Python': 0.06; 'debug': 0.07; 'interpreter.': 0.07; 'lines,': 0.07; 'python3': 0.07; 'subject:code': 0.07; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'discarded': 0.16; 'foo():': 0.16; 'func': 0.16; 'ioerror:': 0.16; 'mode,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'tcl': 0.16; 'thursday,': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'command': 0.22; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'source': 0.25; 'compiled': 0.26; 'this:': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'raise': 0.29; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'inspect': 0.31; 'allows': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; 'could': 0.34; 'display': 0.35; 'there': 0.35; 'skip:" 50': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'more': 0.64; 'subject:there': 0.68; '2014,': 0.84; 'nguyen': 0.84; 'subject:source': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Is there a way to display source code for Python function? |
| Date | Fri, 03 Oct 2014 09:47:31 +0200 |
| Organization | None |
| References | <2d5b0e56-992a-481c-9a8b-eb546cd9ac2e@googlegroups.com> <fb1478cc-e18f-4fd3-a59e-38b7538948a6@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd900d.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14571.1412322468.18130.python-list@python.org> (permalink) |
| Lines | 82 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1412322469 news.xs4all.nl 2894 [2001:888:2000:d::a6]:49104 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:78641 |
Show key headers only | View raw
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]:
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web