How to edit a function in an interactive python session?

Terry Reedy tjreedy at udel.edu
Thu Dec 21 15:11:50 EST 2017


On 12/20/2017 8:42 PM, Peng Yu wrote:

> R has the function edit() which allows the editing of the definition
> of a function. Does python have something similar so that users can
> edit python functions on the fly? Thanks.
> 
> https://www.rdocumentation.org/packages/utils/versions/3.4.3/topics/edit

In general, no.  In general, it is not possible since the definition may 
not be in Python.  (The equivalent must be true in R also.)

In a decent Python IDE, like IDLE, that allows one to enter, retrieve, 
and edit complete statements, then yes for function definitions entered 
in the same session.  It is also possible to edit multi-line code 
line-by-line in the standard REPL, but it is more painful and error prone.

One can monkey-patch an edited function into an imported module written 
in Python.  The original code can be copied from an editor and pasted 
into the REPL.

-- 
Terry Jan Reedy




More information about the Python-list mailing list