Code
Suppose I have:
import numpy
import pickle
class Test():
def __init__(self):
self.base = numpy.zeros(6)
self.view = self.base[-3:]
def __len__(self):
return len(self.view)
def update(self):
self.view[0] += 1
def add(self):
self.view = self.base[-len(self.view) - 1:]
self.view[0] = 1
def __repr__(self):
return str(self.view)
def serialize_data():
data = Test()
return pickle.dumps(data)
Note that class Test is simply a class that contains a view of a NumPy array base. This view is simply a slice of the last N elements in base (N == 3 on initialization).
Test has a method update() which adds 1 to the value at position 0 of the view, and a method add() which modifies the view size (N = N + 1) and sets the value at position 0 to 1.
The function serialize_data simply creates a Test() instance and then returns the serialized object using pickle.
Behavior
If I create a local variable and update it twice and add it once, everything works as expected:
# Local variable
test = Test()
print(test) # [ 0. 0. 0.]
test.update()
test.update()
print(test) # [ 2. 0. 0.]
test.add()
print(test) # [ 1. 2. 0. 0.]
Now, if I create a local variable out of serialized data, then after executing add the value 2 (set after calling update twice) seems to be lost:
# Serialized variable
data = pickle.loads(serialize_data())
print(data) # [ 0. 0. 0.]
data.update()
data.update()
print(data) # [ 2. 0. 0.]
data.add()
print(data) # [ 1. 0. 0. 0.] <---- This should be [ 1. 2. 0. 0. ] !!!
Question
Why is this happening and how could I avoid this behavior?
__getstate__ and __setstate__reference links is better, of course. However, the actual implementation varies depending on the use-case (the case I posted was not the real case I was working with). I will accept your answer within the next 24 hours. I like to keep questions opened for a couple of hours in case someone else jumps in with a different approach. ;-) Thanks again!