How to modify a program while it's running?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Dec 16 20:26:44 EST 2008


On Tue, 16 Dec 2008 13:25:17 -0700, Joe Strout wrote:

> Here's my situation: I'm making an AIM bot, but the AIM server will get
> annoyed if you log in too frequently (and then lock you out for a
> while).  So my usual build-a-little, test-a-little methodology doesn't
> work too well.

Ouch! What AIM server are you running? Perhaps you need a less 
curmudgeonly one?

 
> So I'd like to restructure my app so that it can stay running and stay
> logged in, yet I can still update and reload at least most of the code. 
> But I'm not sure what's the best way to do this.  Should I move the
> reloadable code into its own module, and then when I give my bot a
> "reload" command, have it call reload on that module?  Will that work,
> and is there a better way?

That should work for functions, but less successfully with classes. The 
problem is that existing objects will still have the old behaviour even 
after reloading the class.

In the interactive interpreter, the easiest way around that is to just 
throw the instance away and recreate it. That's not very convenient. You 
may be able to work around that by keeping a list of instances, then 
going through each one and saying:

instance.__class__ = mymodule.MyClass

although I haven't tried this and don't know if it even works.


-- 
Steven



More information about the Python-list mailing list