could that be a mutable object issue ?

P

Philippe C. Martin

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.

Any clue ?

Thanks


Philippe



--
***************************
Philippe C. Martin
SnakeCard LLC
www.snakecard.com
***************************
 
K

Kent Johnson

Philippe said:
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.
... books = []
... ... 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
 
P

Philippe C. Martin

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

Thanks

Philippe


Philippe said:
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.
... books = []
...... 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
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top