[Tutor] Modifying Source Code while Program is Running

Ed Singleton singletoned at gmail.com
Fri Nov 25 12:11:58 CET 2005


On 24/11/05, Kent Johnson <kent37 at tds.net> wrote:
> 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.
Just had a quick look at Smalltalk, and at first glance the overview
of the ideas behind it seems amazing, but the language seems quite
ugly, and it seems to be very IDE led.

> 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.

What I want to do seems quite simple to me in concept, but is seeming
more and more as if it would be hard to implement.

I want to create a small simple CMS for my website.  Users will be
able to add and edit basic pages.  Pages can have sub-pages (no need
for folders cause a folder and an index.html can be unified into one
concept).

Users will also be able to create new types of pages, maybe a
PressReleasePage for example.  PressReleases would be based on a
normal page but might have extra attributes such as Author, Abstract
or DateToBeReleased.

This immediately seemed to me to be a case for classes.  You provide a
way for a user to create a new class by subclassing the page class
(from their point of view probably through adding a few new fields to
a form).  Later if they change their mind they can edit the class by
adding or removing attributes.

However it doesn't really seem that Python is suited to this.  Indeed
it doesn't really seem that Python is suited to a persistent
environment.  Having a program running for a long time (months) is
possible certainly, but it seems to be fighting against the language
rather than working with it.

Ed


More information about the Tutor mailing list