Thanks for the answers, very helpful. I think I'm going to give
Peter's hack a try, as it's actually quite close to what I'm trying to
do -- I get the source for the new function, then that lets me make the
old function become the new one. But I'll probably also use Michael's
solution for class e
Generate your function as a string. Be careful to
indent correctly and append \n at line's end. Then
execute the string with exec(name_of_string). Then
edit your string as necessary, and execute again.
An example follows, directly from one of my projects:
# create a "function" to apply on
Ian Bicking wrote:
> I got a puzzler for y'all. I want to allow the editing of functions
> in-place. I won't go into the reason (it's for HTConsole --
> http://blog.ianbicking.org/introducing-htconsole.html), except that I
> really want to edit it all in-process and in-memory. So I want the
> id
Peter Otten wrote:
(snip)
>
> Can you cheat and just assign another known good func_code object?
def hello(): print "hello"
> ...
def world(): print "world"
> ...
def use_it(hello=hello, world=world):
> ... hello()
> ... world()
> ...
use_it()
> hello
> world
world.func
Ian Bicking wrote:
> I got a puzzler for y'all. I want to allow the editing of functions
> in-place. I won't go into the reason (it's for HTConsole --
> http://blog.ianbicking.org/introducing-htconsole.html), except that I
> really want to edit it all in-process and in-memory. So I want the
> i
"Ian Bicking" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> The func_code attributes of functions is writable, but I don't know how
> to create the proper code object. Just compiling a new body isn't good
> enough.
Did you directly compile the body or compile a function and th
I got a puzzler for y'all. I want to allow the editing of functions
in-place. I won't go into the reason (it's for HTConsole --
http://blog.ianbicking.org/introducing-htconsole.html), except that I
really want to edit it all in-process and in-memory. So I want the
identity of the function to rem