Pickling dynamically generated classes

George Sakkis george.sakkis at gmail.com
Sat Jan 26 02:48:24 EST 2008


The Fine Manual mentions that pickle works for classes that are
defined at the top level of a module. Is there a way to extend this
behavior so that I can pickle and unpickle instances of dynamically
generated classes ?

Longer version: I have a function RecordTypeFactory(fields, ...) that
dynamically generates and caches a record-like class based on the
passed fields and other info (or returns the cached class if it has
been created before). Although I am able to pickle instances of the
various generated classes (records) by writing the gen. class in
globals(), I cannot unpickle them from a different process unless
RecordTypeFactory is called with the same arguments so that "the same"
class is generated in the other process as well. Essentially what I'm
missing is a hook to call RecordTypeFactory with the same fields when
an instance of the gen. class is to be unpickled.

George



More information about the Python-list mailing list