[Numpy-discussion] Possible to pickle new state in NDArray subclasses?
stuart at stuartreynolds.net
Wed Dec 14 14:45:07 EST 2016
I'm trying to subclass an NDArray as shown here:
My problem is that when I save the new class' state with pickle, the new
attributes are lost. I don't seem to be able to override __getstate__ or
__setstate__ to achieve this?
Is it possible to allow new state to serialized when overriding an NDArray?
In my example below, __setstate__ gets called by pickle but not
In the final line, a RealisticInfoArray has been deserialized, but it has
no .info attribute.
import cPickle as pickle
import numpy as np
def __new__(cls, arr, info):
obj = np.asarray(arr).view(cls)
obj.info = info
def __array_finalize__(self, obj):
if obj is None: return
self.info = getattr(obj,"info",None)
def __setstate__(self, *args):
assert False, "EXPLODE"
arr = np.zeros((2,3), int)
arr = RealisticInfoArray(arr, "blarg")
arr2 = pickle.loads(pickle.dumps(arr))
print arr2.info # no .info attribute!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion