Reloading on the fly: was: Re: Python Productivity over C++

Moshe Zadka moshez at math.huji.ac.il
Fri Jun 9 16:53:10 EDT 2000


On 9 Jun 2000, Boudewijn Rempt wrote:

> Could you post an example of designing the main program to reload
> submodules? I'd like to get that working, too, and coming from
> languages where that's impossible, I've a bit of trouble coming up
> with a solution...

You were addressing a different "you", but I would still like to explain how 
I did that in a program that didn't see yet the light of day. I had a 
main module containing "hooks": lists of functions to execute in different
circumstances. For example:

# File: Hooks.py
on_load_hooks = []

Now, when I started up the program, I looked in a specified directory for
extensions: each of them got read into a string, and then I did

exec s in {} 

Now, an extension usually looked like:

#
import Hooks
def foo(params):
	do something
Hoos.on_load_hooks.append(foo)

Additionally, the user was able to add extensions at run-time. There was
no provision for de-registering extensions, except for places which had
place for exactly one hook. In those, registering a hook meant de-registering
the previous one. This convinced me that it is possible, but it wasn't needed
for that application. One caveat: when you de-register a function, first
delete it from the globals, otherwise you might get some nasty leaks. Python
1.6 might solve this if the gc patch gets in.

--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com





More information about the Python-list mailing list