[Chicago] problem with __getstate__

Massimo Di Pierro mdipierro at cs.depaul.edu
Sun May 18 20:58:03 CEST 2008


Odd... this works instead...

from cPickle import *
import copy_reg

class S(dict):
     def __getattr__(self,key): return self[key]
     def __setattr__(self, key, value): self[key]=value

class A(S):
     def __init__(self): self.i=lambda u: u
     #def __getstate__(self): return {}

def unserialize_A(d):
     return A()

def serialize_A(o):
     return unserialize_A, ({},)

copy_reg.pickle(A, serialize_A)

a=A()
print a.i(3)
x=dumps(a)
b=loads(x)
print b.i(4)

I guess the rule is use copy_reg and not __getstate__ if you have  
inheritance.


On May 18, 2008, at 1:44 PM, Massimo Di Pierro wrote:

> does anybody know why the following code does not work?
>
>      from cPickle import *
>
>      class S(dict):
>          def __setattr__(self, key, value): self[key]=value
>      class A(S):
>         def __init__(self): self.i=lambda u: u
>         def __getstate__(self): return {}
>
>      a=A()
>      x=dumps(a)
>
> I know that self.i is a lambda and thus not pickable but __getstate__
> is called (I checked) and thus it should not matter. I guess I do not
> undretansd how pickling under inheritance.
>
> Massimo
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago



More information about the Chicago mailing list