Best way to create a class dynamically on the fly?

Alex Martelli aleax at aleax.it
Mon Sep 30 11:00:45 CEST 2002


Robert Oschler wrote:
        ...
> I forgot to mention, I would like the classes I create dynamically to be
> fully "pickle-able".  If there are any caveats or limitations in this area
> please let me know.

Classes are pickled by name (assuming that using modules pickle or cPickle 
as they stand is what you mean by "fully pickle-able").  So, in general, 
classes you pickle (or, classes whose instance you pickle) must be 
top-level names exported by some module.  Dynamically created classes
obviously don't qualify -- saving all the data needed to recreate such
classes, and recreating them just as dynamically when unpickled, is a
harder problem.  You can register your own arbitrary factories for 
unpickling, but I don't know if that satisfies your criteria for "fully 
pickleable".


Alex




More information about the Python-list mailing list