could that be a mutable object issue ?

Kent Johnson kent37 at tds.net
Sat Feb 19 15:51:18 EST 2005


Philippe C. Martin wrote:
> If I do this:
> 
> 
> 
> print 'LEN OF BOOK BEFORE APPEND: ', len(pickle.dumps(self.__m_rw))
> self.__m_rw.books.append( [p_col1,p_col2,p_col3] )
> print 'LEN OF BOOK AFTER APPEND: ', len(pickle.dumps(self.__m_rw))
> 
> I get the same length before and after append.
> 
> when I print self.__m_rw.books, I see my 'appends' in there, yet the
> pickled object does not change.

How is __m_rw.books defined? If it is a class attribute of the class of __m_rw you will see this 
behavior. e.g.

  >>> class Mrw:
  ...   books = []
  ...
  >>> m=Mrw()
  >>> class Mrw:
  ...   books = []
  ...
  >>> __m_rw = Mrw()
  >>> __m_rw.books.append(1)
  >>> __m_rw.books
[1]

but __m_rw.books will not be pickled with __m_rw because it belongs to the class, not the instance.

The fix is to declare books as an instance attribute:

class Mrw:
   def __init__(self):
     self.books = []

Kent

> 
> Any clue ?
> 
> Thanks
> 
> 
> Philippe
> 
> 
> 



More information about the Python-list mailing list