cPickle and subclassing lists?

Discussion in 'Python' started by Reckoner, Apr 17, 2009.

  1. Reckoner

    Reckoner Guest

    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.
    Reckoner, Apr 17, 2009
    #1
    1. Advertising

  2. Reckoner

    Peter Otten Guest

    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
    Peter Otten, Apr 17, 2009
    #2
    1. Advertising

  3. Reckoner

    Reckoner Guest

    On Apr 17, 10:42 am, Peter Otten <> 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
    Reckoner, Apr 17, 2009
    #3
  4. Reckoner

    Peter Otten Guest

    Reckoner wrote:

    > On Apr 17, 10:42 am, Peter Otten <> 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


    Please post the complete script that provoked the error.
    Does the error occur when you run it from the command line?

    Peter
    Peter Otten, Apr 17, 2009
    #4
  5. >>>>> Reckoner <> (R) wrote:

    >R> I have a large class that is a child of list. I need to pickle it, but
    >R> it's not working. For example, I have reduced it to the following:


    >R> class Mylist(list):
    >R> def __init__(self,x=[]):
    >R> list.__init__(self,x)


    >R> and I cannot even get this to pickle right.


    >>>> w=Mylist([1,2,3])
    >>>> dumps(w)


    >R> PicklingError: Can't pickle <class '__main__.p'>: attribute lookup
    >R> __main__.p fa
    >R> iled


    Where does the 'p' come from?
    What is w.__class__ ?

    >R> I'm using python 2.5 on win32.


    No problem with 2.6.2 on Mac OS X.
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Apr 17, 2009
    #5
  6. Reckoner

    Reckoner Guest

    On Apr 17, 11:16 am, Piet van Oostrum <> wrote:
    > >>>>> Reckoner <> (R) wrote:

    > >R> I have a large class that is a child of list. I need to pickle it, but
    > >R> it's not working. For example, I have reduced it to the following:
    > >R> class Mylist(list):
    > >R> def __init__(self,x=[]):
    > >R> list.__init__(self,x)
    > >R> and I cannot even get this to pickle right.
    > >>>> w=Mylist([1,2,3])
    > >>>> dumps(w)

    > >R> PicklingError: Can't pickle <class '__main__.p'>: attribute lookup
    > >R> __main__.p fa
    > >R> iled

    >
    > Where does the 'p' come from?
    > What is w.__class__ ?
    >
    > >R> I'm using python 2.5 on win32.

    >
    > No problem with 2.6.2 on Mac OS X.
    > --
    > Piet van Oostrum <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email:


    Thanks for all the great feedback. This turns out to be a problem with
    the IPython interpreter,
    and not a Python problem.

    sorry for the confusion.
    Reckoner, Apr 17, 2009
    #6
  7. Reckoner

    Carl Banks Guest

    On Apr 17, 10:13 am, 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.


    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
    Carl Banks, Apr 17, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carsten Gips

    Jython: jythonc and cPickle

    Carsten Gips, Sep 9, 2003, in forum: Python
    Replies:
    0
    Views:
    424
    Carsten Gips
    Sep 9, 2003
  2. Richard Kessler

    cPickle and BOA Constructor hanging!

    Richard Kessler, Feb 11, 2004, in forum: Python
    Replies:
    0
    Views:
    287
    Richard Kessler
    Feb 11, 2004
  3. Simon Dahlbacka

    cPickle and HIGHEST_PROTOCOL problems

    Simon Dahlbacka, Feb 24, 2004, in forum: Python
    Replies:
    0
    Views:
    364
    Simon Dahlbacka
    Feb 24, 2004
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    398
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Daniel Nogradi
    Replies:
    3
    Views:
    347
    Dennis Lee Bieber
    Nov 10, 2006
Loading...

Share This Page