<div dir="ltr"><div>I'm trying to subclass an NDArray as shown here:<br></div><div>   <a href="https://docs.scipy.org/doc/numpy/user/basics.subclassing.html">https://docs.scipy.org/doc/numpy/user/basics.subclassing.html</a><br></div><div><br></div><div>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?</div><div><br></div><div>Is it possible to allow new state to serialized when overriding an NDArray?</div><div><br></div><div>In my example below, __setstate__ gets called by pickle but not __getstate__.</div><div>In the final line, a RealisticInfoArray has been deserialized, but it has no .info attribute.</div><div><br></div><div>----</div><div><br></div><div><div>import cPickle as pickle</div><div>import numpy as np</div><div><br></div><div>class RealisticInfoArray(np.ndarray):</div><div>    def __new__(cls, arr, info):</div><div>        obj = np.asarray(arr).view(cls)</div><div>        <a href="http://obj.info">obj.info</a> = info</div><div>        return obj</div><div><br></div><div>    def __array_finalize__(self, obj):</div><div>        if obj is None: return</div><div>        <a href="http://self.info">self.info</a> = getattr(obj,"info",None)</div><div><br></div><div>    def __setstate__(self, *args):</div><div>        print "SET"</div><div>        return np.ndarray.__setstate__(self,*args)</div><div><br></div><div>    def __getstate__(self):</div><div>        print "GET"</div><div>        assert False, "EXPLODE"<br></div><div>        return np.ndarray.__getstate__(self)</div><div><br></div><div>arr = np.zeros((2,3), int)</div><div>arr = RealisticInfoArray(arr, "blarg")</div><div>print <a href="http://arr.info">arr.info</a></div><div>arr2 = pickle.loads(pickle.dumps(arr))</div><div>print <a href="http://arr2.info">arr2.info</a>  # no .info attribute!</div></div><div><br></div></div>