Mailing List Archive

Display of Python Source
Is there a way to recall and display the python source code from the shell?
For example,

>>> def f():
print "Hello"

>>> f()
Hello
>>>

Now I'd like to load/examine/modify the code segment.

Trying-to-find-the-interpreter-behind-the-p-code-ly yr's,
--

Emile van Sebille
emile@fenx.com
-------------------
Display of Python Source [ In reply to ]
On Wed, 12 May 1999 06:34:57 -0700, "Emile van Sebille" <emile@fenx.com> wrote:

>Is there a way to recall and display the python source code from the shell?
[...]
>Now I'd like to load/examine/modify the code segment.

Hardly, since the "code segment" is byte compiled. It can be disassembled with
the "dis" module (see below), but there is no way to get the source code
(including variable names and comments), even though the translation process is
pretty straightforward.

The best way for your kind of interactive work is propably to choose an IDE,
which allows you to execute arbitrary regions of an editor window. The Emacs
pymode does, IDLE propably also.

Stefan


>>> def f():
... print "Hello"
>>> import dis
>>> dis.dis(f)
0 SET_LINENO 1

3 SET_LINENO 2
6 LOAD_CONST 1 ('Hello')
9 PRINT_ITEM
10 PRINT_NEWLINE
11 LOAD_CONST 0 (None)
14 RETURN_VALUE