After having stored a serialized class object in a file, I would like to provide a static method "Load" which should reopen the file, deserialize the object and return a new instance.
Classically, I would approach this problem by overloading the constructor and pass in all necessary arguments. However, this is not the common in Python but one rather would adapt the following approach:
Python class setup for serialization without pickle
As I deserialize the object in a static method, there is no "cls" and by now I have no clue how to manage it. In short I want to have such a behavior:
x = Foo() # default constructor. All serializable data is created later on
# working with x and creating data
x.save("my_file.pkr")
y = Foo.Load(x)
The load()
function looks like this:
@staticmethod
def load(filename):
with open(filename, 'rb') as bin_object:
lst1 = pickle.load(bin_object)
lst2 = pickle.load(bin_object)
lst3 = pickle.load(bin_object)
lst4 = pickle.load(bin_object)
lst5 = pickle.load(bin_object)
# not possible as I already use the default constructor
return Foo(lst1, lst2, lst3, lst4, lst5)
Can somebody disclose me how to create an object of Foo in the static load method? I still want to create "empty" objects by calling just Foo()
.