Pickle problem

M

Mario Ceresa

Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.
An example session is the following:

Data is A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3] #### I'd like to have here A[1,2,3,4]!!!!!!

Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?

Code follows
-----------------------
class A(object):
objects = []
-----------------------
then I run the code:
---------------------------------------
import pickle
from core import A

a = A()

for i in [1,2,3,4]:
a.objects.append(i)

savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"

print pickle.loads(savedData)
 
G

George Sakkis

Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.
An example session is the following:

Data is  A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3]     #### I'd like to have here A[1,2,3,4]!!!!!!

Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?

Code follows
-----------------------
class A(object):
        objects = []
-----------------------
then I run  the code:
---------------------------------------
import pickle
from core import A

a = A()

for i in [1,2,3,4]:
        a.objects.append(i)

savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"

print pickle.loads(savedData)
--------------------------------------------

Thank you for any help!

Mario

The problem is that the way you define 'objects', it is an attribute
of the A *class*, not the instance you create. Change the A class to:

class A(object):
def __init__(self):
self.objects = []

and rerun it; it should now work as you intended.

HTH,
George
 
M

Mario Ceresa

Dear Jerry and George:
it works like a charm! I always thought that the first way was a
quicker alternative to defining the init method... shame on me!
From now on I'll read the list every day repeating to myself:
"Premature optimization is the root of all evil", "Premature
optimization is the root of all evil", .... :)

Thanks a lot,

Mario


Hello everybody:
I'd like to use the pickle module to save the state of an object so to
be able to restore it later. The problem is that it holds a list of
other objects, say numbers, and if I modify the list and restore the
object, the list itself is not reverted to the saved one, but stays
with one element deleted.

An example session is the following:

Data is A [1, 2, 3, 4]
saving a with pickle
Deleting an object: del a[3]
Now data is A [1, 2, 3]
Oops! That was an error: can you please recover to the last saved data?
A [1, 2, 3] #### I'd like to have here A[1,2,3,4]!!!!!!

Is it the intended behavior for pickle? if so, are there any way to
save the state of my object?

Code follows
-----------------------
class A(object):
objects = []
-----------------------
then I run the code:
---------------------------------------
import pickle
from core import A

a = A()

for i in [1,2,3,4]:
a.objects.append(i)

savedData = pickle.dumps(a)
print "Saved data is ",a
print "Deleting an object"
del a.objects[3]
print a
print "Oops! This was an error: can you please recover the last saved data?"

print pickle.loads(savedData)
--------------------------------------------

Thank you for any help!

Mario

The problem is that the way you define 'objects', it is an attribute
of the A *class*, not the instance you create. Change the A class to:


class A(object):
def __init__(self):
self.objects = []

and rerun it; it should now work as you intended.

HTH,
George
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top