[Tutor] Modifying Source Code while Program is Running

Kent Johnson kent37 at tds.net
Fri Nov 25 14:31:28 CET 2005

Ed Singleton wrote:

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

Yes, that will work. You can change the definition of a class method at runtime.

> 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

Yes, you can add attributes to classes and objects.

Did you look at this recipe?

The MetaInstanceTracker metaclass gives an easy way for classes to keep track of their instances. The MetaAutoReloader metaclass uses MetaInstanceTracker to update class definitions when a module is reloaded.
> 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.

That seems rather idealistic!
> 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).

I still think you are shooting for too high a level of abstraction. 


More information about the Tutor mailing list