cPickle and subclassing lists?

Reckoner reckoner at gmail.com
Fri Apr 17 13:51:18 EDT 2009


On Apr 17, 10:42 am, Peter Otten <__pete... at web.de> wrote:
> 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

sorry. here it is:

PicklingError: Can't pickle <class '__main__.Mylist'>: attribute
lookup __main__.Mylist failed





More information about the Python-list mailing list