How to load new class definitions at runtime?
carribeiro at gmail.com
Mon Nov 15 16:04:39 CET 2004
On Sun, 14 Nov 2004 10:07:01 +0100, Alex Martelli <aleaxit at yahoo.com> wrote:
> Carlos Ribeiro <carribeiro at gmail.com> wrote:
> > attached to it. So simple object instances would not cut it. To allow
> > the system to be customized without taking the application server
> > offline, there must be a solution for these new class definitions to
> > be loaded at runtime.
> If you need them to be _updatable_ at runtime, look at a recipe by
> Michael Hudson in the cookbook -- I've updated it for the printed
> version 2nd ed (since we do have __subclasses__ now, yay!) but that's
> not done yet. My vote for most useful custom metaclasses ever...
Thanks for the pointer. Michael Hudson's recipe solves one huge part
of the problem; I was not even trying to solve it yet, but it helps a
> It's a harder deployment decision whether the normal system operation
> should also support the filesystem, or just the database.
I'm thinking about two alternatives right now, not mutually exclusive.
1) I'm trying to understand how do packages work, and what hooks are
available. I guess I could make it work using packages to re-read the
source code whenever a "from <package> import <module>" statement is
executed; Michael Hudson's recipe could also be automatically invoked
for the redefined classes.
2) Another suggestion is to use a source-code control system as the
code repository. For example: using the Python Subversion interface, I
could checkout the code for the class. It's safer, in a sense, than
database access, because of the way it tracks changes and logs
accesses. But it's also more complex, and possibly slower, than a file
read, or even a database read (depending on the db engine, of course).
My choice now is a simple-minded 'always re-read the source code'
approach, just to make it work and test some concepts. But the problem
seems interesting enough to deserve further investigation. I'm sure a
"dynamic package" version, with automatic redefinition using the
Michaels Hudson's recipe, would make for a good recipe itself.
Consultoria em Projetos
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com
More information about the Python-list