could that be a mutable object issue ?

Philippe C. Martin philippe at philippecmartin.com
Sat Feb 19 19:29:36 EST 2005


You are correct and I still don't know Python (sigh).

Thanks

Philippe


On Sat, 19 Feb 2005 15:51:18 -0500, Kent Johnson wrote:

> 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