Let me rephrase the first sentence of my last post: I don't think the issue is inheritance of just *any* class, but specifically inheritance of built-in types. :)<br><br>- Atul<br><br><div class="gmail_quote">On Sun, May 18, 2008 at 12:06 PM, Atul Varma <<a href="mailto:varmaa@gmail.com">varmaa@gmail.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I could be wrong, but I don't think the issue is inheritance--I think it's actually because you're subclassing a built-in type, which requires doing some special stuff with pickle (namely, registering with copy_reg as you've done in your last email).<br>
<br>If you use pickle instead of cPickle, you get a more informative traceback; take a look at pickle.Pickler and notice how the dispatch table, which appears to map types to serialization functions, maps the dictionary type to Pickler.save_dict(). I think that has something to do with it.<br>
<br>Hope that helps... I have to run so I can't look into this more right now, but let me know if you'd like me to.<br><font color="#888888"><br>- Atul</font><div><div></div><div class="Wj3C7c"><br><br>On Sun, May 18, 2008 at 11:58 AM, Massimo Di Pierro <<a href="mailto:mdipierro@cs.depaul.edu" target="_blank">mdipierro@cs.depaul.edu</a>> wrote:<br>
<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Odd... this works instead...<br>
<br>
from cPickle import *<br>
import copy_reg<br>
<br>
class S(dict):<br>
def __getattr__(self,key): return self[key]<div><br>
def __setattr__(self, key, value): self[key]=value<br>
<br>
class A(S):<br>
def __init__(self): self.i=lambda u: u<br>
#def __getstate__(self): return {}<br>
<br></div>
def unserialize_A(d):<br>
return A()<br>
<br>
def serialize_A(o):<br>
return unserialize_A, ({},)<br>
<br>
copy_reg.pickle(A, serialize_A)<br>
<br>
a=A()<br>
print a.i(3)<br>
x=dumps(a)<br>
b=loads(x)<br>
print b.i(4)<br>
<br>
I guess the rule is use copy_reg and not __getstate__ if you have inheritance.<div><div></div><div><br>
<br>
<br>
On May 18, 2008, at 1:44 PM, Massimo Di Pierro wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
does anybody know why the following code does not work?<br>
<br>
from cPickle import *<br>
<br>
class S(dict):<br>
def __setattr__(self, key, value): self[key]=value<br>
class A(S):<br>
def __init__(self): self.i=lambda u: u<br>
def __getstate__(self): return {}<br>
<br>
a=A()<br>
x=dumps(a)<br>
<br>
I know that self.i is a lambda and thus not pickable but __getstate__<br>
is called (I checked) and thus it should not matter. I guess I do not<br>
undretansd how pickling under inheritance.<br>
<br>
Massimo<br>
_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/chicago" target="_blank">http://mail.python.org/mailman/listinfo/chicago</a><br>
</blockquote>
<br>
_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/chicago" target="_blank">http://mail.python.org/mailman/listinfo/chicago</a><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>