[C++-sig] pickling new-style classes object

Dmitri Mouromtsev dimour at mail.ru
Wed Oct 20 14:22:51 CEST 2004

Hello All,

Is it possible to customize pickling for class objects?
or these are pickled only by name using default implementation.

I need to save and restore custom class definition in different Python
sessions. For example:

  class Prototype(type):
    def __init__(cls, name, bases, dict):
      super(Prototype, cls).__init__(name, bases, dict)

  class C(object):
    __metaclass__ = Prototype

    def __init__(self, name):
      self.__name__ = name

If I define new class

  class New(C): pass
  New.a = 35
  New.b = 'string'

and new instance of 'New':

  x = New('New')

And trying to save 'New' and 'x':

  file = open('pickle_New.txt', 'w')
  dump(New, file)
  file = open('pickle_x.txt', 'w')
  dump(x, file)

I have only 'x' state saved.
__getstate__ and __setstate__ are not called for class instances(New).
What is the problem? Is there another way to resolve this problem?


More information about the Cplusplus-sig mailing list