On 24/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote:
> There are many ways of doing this, few of them very nice IMHO.
>
> > without having to restart the program?  Is it feasible to get a
> > program to change it's own source code while it is running?
>
> Yes, you can overwrite an existing module then call reload
> from within the program.
>
> > 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.
>
> Yes that would be feasible., preferrably in response to an admin
> page where you could fill in a list of modules to be reloaded...
> But you neeed to be very careful not to break any of the interfaces,
> the Liskov Substitution Principle must be strictly observed.
>
> > to allow users of the site to edit a class through the web and
> > see the changes to the site immediately?
>
> Extremely dangerous but the same principle would apply,
> simply load the existing module into an editor pane then save
> the modified version and reload it.
>
> > Can a python program change a class,
>
> Yes as above.
>
> > change all the objects already created by that class
>
> Trickier and potentially involves some low level poking that
> should not be encouraged IMHO! :-)

> > 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).
>
> If the LSP is adhered to its feasible but I don't intend to try any
> such thing! It would be full of pitfalls and an almosyt certain recipe
> for reliability problems that would be impossible to find.
> Self modifying code sounds like a good idea but there is a very
> good reason why its almost never used in production software!

Well, self-modifying isn't inherently necessary.  What I guess I
really need is persistent classes as well as persistent objects.

I always tend to think of classes as templates for objects rather than
factories.  In my mind, object methods are just calls to the class
which are evaluated every time they are called.  Objects should be
strict instances of classes that can't be modified except for the
values of their attributes.

I think I can actually achieve this to some degree by doing:

Class Page(object):
    def print(self):
        printPage(self)

And have all my methods call functions (passing on parameters as
necessary).  That way if I change a function, it will be changed for
every instance of every object of that class.

And couldn't I write a function that would add functions or attributes
to classes and objects?

def addAttribute(class, attribute, starting value):
    # add it to the class
    # iterate through all objects already created by the class
        # add attribute to object

Am I trying to use the wrong language for this?  I love Python but I
seem to keep coming up against lots of practical issues with it and I
really don't want to bother with practical issues.  I just want to
define the behaviours I want without having to bother with how the
computer is actually going to handle them.

I guess it's very much a "I don't care how it works!" attitude, which
is probably a corollary to "premature optimisation is the root of all
evil".  Ignore all issues of memory and speed and create something
highly abstract that allows you to define your solution.  Then work
down from there and start worrying about speed and memory and
practical issues later (or hopefully never).

Ed
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to