[Python-Dev] Re: [Import-sig] Re: Proposal for a modified import mechanism.

Just van Rossum just@letterror.com
Wed, 14 Nov 2001 22:07:22 +0100


Greg Ward wrote:

>     More importantly, it is fundamentally impossible for module
>     reloading to work in Python.  Believe me, I've tried several times,
>     and each time I run up against the same brick wall: [ ..... ]
> 
> If anyone has a solution to this, I'm all ears, but for now I'm pretty
> well convinced that it cannot be done.

Has anyone ever tried something like this:

    make a copy of module.__dict__
    module.__dict__.clear()
    execute source in module.__dict__
    for each function in module:
        assign new func attrs to *old* func object
            (possible for func_code, func_defaults, func_doc and func_dict)
        inject old (but updated!) func object back into module.__dict__
    for each class in module:
        for each func in class:
            <same as global functions>
        # XXX something with __bases__...

?

This way, at least all functions and classes will be the *same* objects as
before, but updated.

What I use a lot is reloading individual methods (possible in the MacPython IDE
by selected a method and "run" the selection). This has some serious
disadvantages: 
- linenumbers on code below the updated method(s) won't be correct anymore
- references to bound methods still reference the *old* method.
Still, I have found this to be an enormous productivity gain: you're really
hacking on live objects. Maybe a scheme like the one above can make this even
more transparent?

Just