cPickle and subclassing lists?

Carl Banks pavlovevidence at gmail.com
Fri Apr 17 17:07:50 EDT 2009


On Apr 17, 10:13 am, Reckoner <recko... at gmail.com> 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.

It worked for me.

Wild guess: you've defined Mylist in a module (or, rather, you defined
class p in an module and assigned Mylist to it), but you are exec'ing
the module rather than importing it, ostensibly because importing a
module won't load any changes you've made into the interpreter.  Well,
doing that will cause funny things to happen when pickling.  If you're
doing that, consider the reload function instead (although it has it's
own problems).

I'd highly recommend against pickling an instance of a class that
isn't defined in, and loaded from, a regular module.  Don't pickle
instances of classes defined at the interpreter or through exec.


Carl Banks



More information about the Python-list mailing list