A pickle problem!
Ian Kelly
ian.g.kelly at gmail.com
Fri Apr 22 12:27:34 EDT 2016
On Thu, Apr 21, 2016 at 7:52 PM, Paulo da Silva
<p_s_d_a_s_i_l_v_a_ns at netcabo.pt> wrote:
> Às 22:43 de 21-04-2016, Paulo da Silva escreveu:
>> Hi.
>>
>> Why in this code fragment self.__name is not kept between pickle
>> dumps/loads? How to fix it?
>>
>> Thanks.
>>
>> import pickle
>> import pandas as pd
>> import numpy as np
>>
>> class C(pd.DataFrame):
>> def __init__(self,name,*a,**b):
>> super(C,self).__init__(*a,**b)
>> self.__name=name
>>
>> def GetName(self):
>> return self.__name
>>
> # Adding this works but looks tricky!
>
> def __getstate__(self):
> dfstate=super(C,self).__getstate__()
> cstate=(dfstate,self.__name)
> return cstate
>
> def __setstate__(self,cstate):
> super(C,self).__setstate__(cstate[0])
> self.__name=cstate[1]
Probably this is necessary because the DataFrame class is already
customizing its pickle behavior without taking into account the
possibility of added attributes by subclasses. I think that your
solution of wrapping the state of the superclass looks fine.
More information about the Python-list
mailing list