[IPython-dev] Changing functions in a running program?

Thomas Kluyver takowl at gmail.com
Sun Jan 13 03:13:04 EST 2019


Hi Andreas,

If you define a function or variable at the breakpoint, it's probably
making it a local variable inside main(), so it's not in scope for name().

You may be able to get round this by defining the new function and then
explicitly making it a global variable, something like this:

globals()['print_name'] = print_name

Not exactly elegant, but hopefully it works.
Thomas

On Sun, 13 Jan 2019 at 00:10, Andreas Yankopolus <andreas at yank.to> wrote:

> Is it possible to run a program to a breakpoint in IPython and then change
> functions within that program? This would be for interactively
> experimenting with program logic without having to restart the program from
> scratch. I’m running IPython3 from Emacs using Elpy for interacting with
> the IPython3 process.
>
> Consider foo.py as follows:
>
> #!/usr/bin/env ipython3
>
> import sys
> from IPython.core.debugger import Pdb
>
>
> def print_name():
>     print ("Alice")
>
> def name():
>     print_name()
>
> def main(argv):
>     print ("In main.")
>     Pdb().set_trace()
>     name()
>
> if __name__ == "__main__":
>     main(sys.argv[1:])
>
> Running it gives me:
>
> *ayank at snorri*:*~/Documents*$ ./foo.py
> In main.
> > /home/ayank/Documents/foo.py(16)main()
>      14     print ("In main.")
>      15     Pdb().set_trace()
> ---> 16     name()
>      17
>      18 if __name__ == "__main__":
>
>
> ipdb>
>
> I now want to change the definition of print_name() to print “Bob”
> instead of “Alice”. I can make this change in Emacs, send the new function
> to IPython with C-c C-y f, but when I then type name(), I get “Alice”.
> The reference to print_name() in name() is not getting updated to point
> to the new definition of print_name(). Likely I’m going about this
> process incorrectly, as I can make these kind of changes at an IPython3
> prompt but not at an ipdb one.
>
> Thanks,
>
> Andreas
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at python.org
> https://mail.python.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20190113/06d1bcdb/attachment.html>


More information about the IPython-dev mailing list