[Tutor] Modifying Source Code while Program is Running

Kent Johnson kent37 at tds.net
Thu Nov 24 17:49:54 CET 2005

Ed Singleton wrote:
> Is it feasible to change a program's source code whilst it is running
> without having to restart the program?  Is it feasible to get a
> program to change it's own source code while it is running?

You can change a class while it is running.
> For example, if you have a web server such as CherryPy that will
> (hopefully) be running for months at a time and you want to be able to
> change classes without having to restart the server.  Or if you want
> to allow users of the site to edit a class through the web and see the
> changes to the site immediately?

The auto-restart feature of CherryPy might do this for you. Also if the changes to the site are to a template such as Cheetah, those usually autoreload.
> Can a python program change a class, change all the objects already
> created by that class and save the modified class definition, so that
> if the program were restarted it would return to exactly the same
> state? (assuming all objects were saved to a database or somesuch).

You can have persistent objects using for example SQLObject or ZODB,
> Does anyone have any good links to implementations of this?  I assume
> someone's already done it before.

It sounds like maybe you come from a background in Smalltalk, or maybe you should look at Smalltalk. In Smalltalk the whole environment is dynamic and can be saved and restored easily.

For Python, I think you will do better if you narrow your requirements. Python is very dynamic - classes can be changed at runtime, or reloaded if you are careful - and there are several good ways to persist state. If you can be more specific about what you really need there may be a solution for you.


More information about the Tutor mailing list