Automatic reloading, metaclasses, and pickle

Ziga Seilnacht ziga.seilnacht at gmail.com
Tue Feb 27 15:50:37 EST 2007


Andrew Felch wrote:
> Hello all,
>
> I'm using the metaclass trick for automatic reloading of class member
> functions, found at:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164
>
> My problem is that if I
> 1) pickle an object that inherits from "AutoReloader"
> 2) unpickle the object
> 3) modify one of the pickled' object's derived class methods
> 4) reload the module holding the class
>
> ... then the changes don't affect the unpickled object.  If I unpickle
> the object again, of course the changes take effect.
>
> My friend that loves Smalltalk is laughing at me.  I thought I had the
> upperhand when I discovered the metaclasses but now I am not sure what
> to do.  I really don't want to have to unpickle again, I'm processing
> video and it can take a long time.
>
> By the way, I used to avoid all of these problems by never making
> classes, and always building complex structures of lists,
> dictionaries, and tuples with global functions.  It's going to take me
> a while to kick those horrible habits (during my transition, I'm
> deriving from list, dict, etc. hehe), perhaps a link to the metaclass
> trick is in order in the tutorial's comments on reload?
>
> Any help that avoids having to unpickle again is appreciated!
>
> Thanks,
> Andrew Felch

This happens because unpickling doesn't recreate your object by
calling its type. MetaInstanceTracker registers an instance only
when it is created by calling a class.

You can solve this by moving the instance registration to
AutoReloader.__new__ and using pickle protocol version 2, but the
best solution is to avoid both pickle (old pickles break if you
change your code) and autoreloading (it's meant to be used in
interactive console and entertaining ircbots, not in normal code).

Ziga




More information about the Python-list mailing list