Changing Python Code In The Debugger
Solution 1:
Since you can change the contents of regular classes the way you want at any time, there's no need to update references to objects: you just update class's __dict__
with new methods and other attributes.
The problem is with references to functions: you can't update a function without changing its identity. You can use proxy functions that always look up real functions by name, and you change real functions at any moment. Else, you plan your code so that it doesn't store function references for long; once a function is updated, it will soon be looked up by name, but old references passed somewhere will continue to execute for a bit longer.
Such patching would make sense on a long-running system when you want to upgrade it without serious downtime: you pause it for a moment to upgrade several classes and functions consistently and un-pause. AFAIK Erlang does its on-the-fly updates in a similar way.
Solution 2:
Yes, pdb can do this. Although you have to do the editing in another editor, and the changes will be ignored until you restart.
But since all you want to do is small changes, this is not a problem. But you can't change the running code (except with reload, see below), as changing the code would mean the code and the state is out of sync.
What you can do with a debugger is test the change you want to do. You can paste in the code as you want to change it, and thus test that it is correct without rerunning the whole program. But in that case you don't edit the file.
(In some specific cases you might be able to get away with not restarting by careful use of "reload()", but that's probably not worth the effort.)
Solution 3:
You can update the code in a running python process with xreload:
http://svn.python.org/projects/sandbox/trunk/xreload/xreload.py
There are many limitations, they are listed at the top of the file. I'm not sure if this handles the case you want though - do you want to actually prevent the exception from propagating? That requires more than updating the running program.
Solution 4:
whether it's theoretically possible to allow changes to Python code
Yes. It's possible to update a code object "on the fly". Nothing in the Python VM seems to prevent this. There's not an ongoing verification of the checksums or anything.
somehow update all the references to the objects that changed,
This doesn't even make sense. Once the class-level method definition is changed, "updating" all the references isn't even necessary or sensible. The code is changed in the one and only place it exists.
This is why debuggers are bad. They seem to lead to murky, unclear thinking. Thinking too much about the debugger means thinking too little about the real problem at hand.
TDD is a far, far better investment. Small, manageable unit tests run quickly and provide enduring evidence that things work.
Solution 5:
name = (input("Enter Name: "))
if name == "e": print ("This word has a letter e")
else : print("This word has no letter e")
Post a Comment for "Changing Python Code In The Debugger"