Other modules can be imported to sandbox (you mean modules that are created dynamically at runtime) by
sandbox.other_module = __import__('other_module')
or:
exec 'import other_module' in sandbox.__dict__
If you call "sandbox" modules from other modules or other sandbox modules and you want to reload some new code later, it is easier to import only a module, not names from it like "from sandbox import f", and call "sandbox.f" not "f". Then is reloading easy. (but naturarely reload command is not useful for it)
Classes
>>> class A(object): pass
...
>>> a = A()
>>> A.f = lambda self, x: 2 * x # or a pickled function
>>> a.f(1)
2
>>> A.f = lambda self, x: 3 * x
>>> a.f(1)
3
It seems that reloading methods can be easy. I remember that reloading classes defined in a modified source code can be complicated because the old class code can be held by some instance. The instance's code can/need be updated individually in the worst case:
some_instance.__class__ = sandbox.SomeClass # that means the same reloaded class
I used the latter with a python service accessed via win32com automation and reloading of classes code was succesful without loss instances data
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…