cPickle and subclassing lists?

Peter Otten __peter__ at web.de
Fri Apr 17 19:42:53 CEST 2009


Reckoner wrote:

> I have a large class that is a child of list. I need to pickle it, but
> it's not working. For example, I have reduced it to the following:
> 
> class Mylist(list):
>     def __init__(self,x=[]):
> list.__init__(self,x)
> 
> and I cannot even get this to pickle right.
> 
>>> w=Mylist([1,2,3])
>>> dumps(w)
> 
> PicklingError: Can't pickle <class '__main__.p'>: attribute lookup
> __main__.p fa
> iled
> 
> 
> I'm using python 2.5 on win32.
> 
> any help appreciated.

This error occurs when you try to pickle a class that cannot be found by its
name:

>>> from cPickle import dumps
>>> class p: pass
...
>>> a = p
>>> del p
>>> dumps(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cPickle.PicklingError: Can't pickle __main__.p: attribute lookup __main__.p
failed

I don't see how this error could be triggered by the code you give above.
Please try it again in a fresh interpreter.

Peter



More information about the Python-list mailing list