pickling sublcass of gobject.GObject?

Jp Calderone exarkun at divmod.com
Tue Jul 27 14:04:09 EDT 2004


Skip Montanaro wrote:
> I have a class that inherits from PyGTK's gobject.GObject.  I thought to
> pickle it I'd be able to just define __[gs]etstate__ methods.  When I
> attempt to dump an instance of this class I get a TypeError from
> copy_reg._reduce_ex telling me that GObject.__init__() takes no args but
> that it was called with one arg.  Here's a "hello world" example:
> 

   Here's an example:

         >>> def makeAC(x):
         ...     c = C()
         ...     c.x = x
         ...     return c
         ...
         >>> class C(gobject.GObject):
         ...     def __init__(self):
         ...             self.__gobject_init__()
         ...             self.x = 5
         ...     def __reduce__(self):
         ...             return makeAC, (self.x,)
         >>> pickle.loads(pickle.dumps(C()))
         <C object (GObject) at 0x401f1d74>
         >>> pickle.loads(pickle.dumps(C())).x
         5

   Generally, pickle treats __reduce__ kind of like this:

     results = o.__reduce__()
     # Later
     o = results[0](*results[1])
     if len(results) == 3:
         o.__setstate__(results[2])

   Jp




More information about the Python-list mailing list