pickling a circular object inherited from list

Klaus Kopec klaus.kopec at tuebingen.mpg.de
Tue Dec 9 16:59:16 CET 2008

Hello everyone,

I have a problem with inheritance from list. I want to create a tree 
like object where child nodes are kept in self[:] and every child has a 
field that points to its parent. Pickling such an object, however, 
throws an AssertionError. See below for source code and output of an 
easy example case of my problem.

What did I do wrong?

Best regards,

======== source:
class myList(list):

class myObject:
     def __init__(self, parent=None):
         self.parent = parent

if __name__ == '__main__':
     r = myList()
     r.append( myObject(r) )

     from pickle import dump, load
     dump(r, file('/tmp/x', 'w'))

========= output:
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/tmp/python-11053-o.py", line 15, in ?
     dump(r, f)
   File "/usr/lib/python2.4/pickle.py", line 1382, in dump
     Pickler(file, protocol, bin).dump(obj)
   File "/usr/lib/python2.4/pickle.py", line 231, in dump
   File "/usr/lib/python2.4/pickle.py", line 338, in save
     self.save_reduce(obj=obj, *rv)
   File "/usr/lib/python2.4/pickle.py", line 419, in save_reduce
   File "/usr/lib/python2.4/pickle.py", line 251, in memoize
     assert id(obj) not in self.memo

More information about the Python-list mailing list