Pickling array.array

Nicolas Fleury nid_oizo at yahoo.com_remove_the_
Fri May 14 14:47:06 EDT 2004


Nicolas Fleury wrote:
> For those interested in the same problem, the only solution I've found 
> is to encapsulate the array instead of derive from it.  It sucks a bit, 
> since all methods need to be implemented to redirect to array.array, but 
> at least it's working.  I'm still wondering how to do it with derivation...

Here's a complete solution (if anyone knows a simpler solution...):

import array as arr
class array:
     def __init__(self, typecode, initializer=None):
         self.arr = arr.array(typecode, initializer)
     def __getstate__(self):
         return {'buffer': self.arr.tostring(),
                 'typecode': self.arr.typecode}
     def __setstate__(self, dict):
         self.arr = arr.array(dict['typecode'])
         self.arr.fromstring(dict['buffer'])
     def __getattr__(self, name):
         if name == 'arr': return self.arr
         return self.arr.__getattribute__(name)
     def __setattr__(self, name, value):
         if name == 'arr': self.__dict__['arr'] = value
         else: self.arr.__setattr__(name, value)




More information about the Python-list mailing list