I wonder if it is possible somehow to read the pickle file correctly without providing MyClass code.
import pickle
class MyClass:
def __init__(self, n):
self._n = n
def give_vec(self):
return [1 for _ in range(self._n)]
if __name__ == '__main__':
m = MyClass(30)
with open('test.pickle', 'wb') as p:
pickle.dump(m, p)
Assume we have run the code above and now start a python interpreter and try the following
>>> import pickle
>>> p = open('test.pickle', 'rb')
>>> a = pickle.load(p)
So that we won't get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Can't get attribute 'MyClass' on <module '__main__' (built-in)>
The idea is to provide a pickle that has some interface without specifying the exact class