cPickle and subclassing lists?

R

Reckoner

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.
 
P

Peter Otten

Reckoner said:
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:
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
 
R

Reckoner

Reckoner said:
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:

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
 
P

Peter Otten

Reckoner said:
Reckoner said:
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
 
P

Piet van Oostrum

Reckoner said:
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.

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.
 
R

Reckoner

Reckoner <[email protected]> (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.

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.
 
C

Carl Banks

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top