Pickling dynamically generated classes?

后端 未结 6 791
野性不改
野性不改 2020-12-24 11:48

I\'m using type() to dynamically generate classes that will ultimately be pickled. The problem is that the un-pickling process needs the definition of the class

6条回答
  •  死守一世寂寞
    2020-12-24 12:32

    One idea would be to pickle a tuple with:

    1. The name of the dynamic class
    2. The subclass tuple (possibly in string form from repr())
    3. The class dictionary
    4. The actual instance

    This would allow you to pickle a class and then reconstruct it later using type() and subclassing Unpickler.

提交回复
热议问题