Reload Tricks

Michael Spencer mahs at telcopartners.com
Sat Jan 22 12:50:21 EST 2005


Kamilche wrote:
> I want my program to be able to reload its code dynamically. I have a
> large hierarchy of objects in memory. The inheritance hierarchy of
> these objects are scattered over several files.
> 
Michael Spencer wrote:
> An alternative approach (with some pros and cons) is to modify the class in place, using something like:
> 
>  >>> def reclass(cls, to_cls):
>  ...     """Updates attributes of cls to match those of to_cls"""
>  ...
>  ...     DONOTCOPY = ("__name__","__bases__","__base__",
>  ...                     "__dict__", "__doc__","__weakref__") 
etc...

Kamilche wrote:
> Would it be possible to just not copy any attribute that starts and
> ends with '__'? Or are there some important attributes being copied?


Possible?  of course, it's Python ;-)

But there are many 'magic' attributes for behavior that you probably do want to 
copy:

e.g., __getitem__, __setitem__ etc...

See: http://docs.python.org/ref/specialnames.html

Michael Hudson's recipe: 	 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164
does auto-reloading "automatically", at the price of changing the type of the 
classes you want to manage.  It's a very convenient approach for interactive 
development (which is the recipe's stated purpose).  It works by tracking 
instances and automatically updating their class.  If your program relies on 
class identity, you may run into problems.


Michael





More information about the Python-list mailing list