Questions re subclassing at "load time"

Edward K. Ream edreamleo at charter.net
Sun Jun 22 07:55:06 EDT 2003


There appears to be an easy way for plugins to override classes in an app.
This seems like a spectacular capability.  I'd like your comments on the
following scheme.

1. In my app, a plugin is simply a .py file that gets "loaded", i.e.
imported, at a particular time while my app is starting up.  My app loads
plugins before creating any objects whose classes may be modified by
plugins.

2. My app defines each class that may be subclassed by plugins using the
following pattern:

class aCoreClass:
    << methods of aCoreClass >>

class aClass (aCoreClass):
    pass

3. Except as above, all code in my app refers to aClass, not aCoreClass.

4. To modify aClass, a plugin does the following at the outer level:

import aClass  # The file containing aCoreClass and aClass.

class myNewClass (aClass.aCoreClass):
    << overriding methods of aCoreClass >>

aClass.aClass = myNewClass # change what an aClass is!

Again, this code will be executed when the plugin gets loaded.

5. The plugin code must be executed before any objects of aClass have been
created.  This is true in my app because plugins are loaded early.

This seems like a much easier way to modify classes than:

def funcToMethod(f,theClass,name=None):
    setattr(theClass,name or f.__name__,f)

Initial experiments appear to show that this scheme works well.  My
questions:

1. Is this scheme completely safe?  Anything to watch out for?

2. Any suggestions, improvements or simplifications?

Thanks very much.

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list