Arnaud Delobelle escreveu: > On Mar 17, 9:31 pm, Paulo da Silva <[EMAIL PROTECTED]> wrote: ...
>> I used __new__ to >> subclass date class but now cPickle/pickle loads >> does not work. >> >> from datetime import date >> import cPickle,string >> >> class MyDate(date): >> def __new__(cls,year,month=None,day=None): >> if type(year) is str: >> year,month,day=map(int,string.split(year,'-')) >> if year<100: >> year+=2000 >> return date.__new__(cls,year,month,day) >> >> class C1(object): >> def __init__(self): >> self.x=MyDate("2007-3-15") >> >> def f(self): >> print self.x >> >> c1=C1() >> >> d=cPickle.dumps(c1) >> c2=cPickle.loads(d) >> c2.f() > > I haven't tried your code but I think that you may need to define a > __reduce__ method in your MyDate class in order to give a clue to the > python as to how to pickle its instances. For more details see: > > http://docs.python.org/lib/node321.html > > Something like: > > class MyDate(date): > ... > def __reduce__(self): > return type(self), (self.year, self.month, self.day) > > might solve your problem. > > HTH > > -- > Arnaud > Thanks. This works exactly the way you wrote. Yet I am misunderstanding something. Can't pickle "see" that being MyDate derived from date it also has to look at variables from date? When do I need to do this? I am using pickle with a lot more complex classes without this problem. Thank you Paulo -- http://mail.python.org/mailman/listinfo/python-list