Python类设置为没有pickle的序列化 [英] Python class setup for serialization without pickle
问题描述
场景
我在python中寻找一个面向对象的方法,使得可以在数据文件中保存一个类的实例,在稍后的时间点。我现在的方法如下:
class A(object):
def __init __(self,ComplexParam1,ComplexParam2) :
self.ComplexParam1 = ComplexParam1
self.ComplexParam2 = ComplexParam2
@staticmethod
def创建(EasyParam1,EasyParam2):
#do一些复杂的计算从EasyParam1和EasyParam2获取ComplexParam1和ComplexParam2
return A(ComplexParam1,ComplexParam2)
def保存(self,Filename):
#write ComplexParam1和ComplexParam2 to disk
@staticmethod
def Load(Filename):
#read ComplexParam1和ComplexParam2和调用构造函数
return A(ComplexParam1,ComplexParam2)
正如你可以看到 ComplexParam1
和 ComplexParam2
是要计算的参数,不用于对象 A
的第一次创建,因为它们非常复杂,而 EasyParam1
和
EasyParam2
是已知参数。认为它好像 EasyParameters
是整数, ComplexParameters
是基于<$ c $构造的大型基数c> EasyParameters
所以我使用上面的设置保存
c $ c>加载对象到/从文件,其中创建
使用构造函数,因为 ComplexParam1
和 ComplexParam2
存储在文件中,不需要再次计算。
问题
到目前为止,上面显示的方法对我来说很好。然而,当该方案也与类继承一起使用时,会出现问题。所以我正在寻找一个更好的和更清洁的解决方案,我的问题。
在C + +我将重载构造函数,并使两个可能的类的创建,但这不是
任何帮助,链接和建议。
我认为这是 @classmethod的情况
装饰器。例如,如果将 Load
方法更改为以下内容:
@classmethod
def Load(cls,Filename):
#Do stuff
return cls(ComplexA,ComplexB)
然后你可以覆盖构造函数:
B类$ b def __init __(self,complexA,complexB):
#无论你想要什么,包括调用父构造函数
最后,你可以调用
B .__ init __
的B.Load(some_file)
。Scenario
I am looking for an object oriented approach in python that makes it possible to save an instance of a class in a data file and also load it again in at a later point in time. My current approach looks like this:
class A(object): def __init__(self, ComplexParam1, ComplexParam2): self.ComplexParam1 = ComplexParam1 self.ComplexParam2 = ComplexParam2 @staticmethod def Create(EasyParam1, EasyParam2): #do some complex calculation to get ComplexParam1 and ComplexParam2 from EasyParam1 and EasyParam2 return A(ComplexParam1, ComplexParam2) def Save(self, Filename): #write ComplexParam1 and ComplexParam2 to disc @staticmethod def Load(Filename): #read ComplexParam1 and ComplexParam2 and call constructor return A(ComplexParam1, ComplexParam2)
As you can see
ComplexParam1
andComplexParam2
are to be calculated parameters and are not used for a first creation of the objectA
since they are very complex to get, whereasEasyParam1
andEasyParam2
are "known" parameters. Think of it as if theEasyParameters
are integers and theComplexParameters
are large matricies that are constructed based onEasyParameters
So I am using the setup above to
Save
andLoad
objects to and from file, whereCreate
uses the constructor sinceComplexParam1
andComplexParam2
are stored in file and do not need to be calculated again.Problem
Up until now, the approach shown above worked just fine for me. Problems however arise, when this scheme is also used with class inheritances. So I am looking for a nicer and cleaner solution to my problem.
In C++ I would overload the constructor and make two possible creations of the class available, but this is not supported in python.
Any help, link and suggestion is appreciated.
解决方案I think this is a case for the
@classmethod
decorator. For example, if you change theLoad
method to the following:@classmethod def Load(cls, Filename): # Do stuff return cls(ComplexA, ComplexB)
Then you could override the constructor:
class B(A): def __init__(self, complexA, complexB): # Whatever you want, including calling the parent constructor
And, finally, you could call
B.Load(some_file)
which would invokeB.__init__
.这篇关于Python类设置为没有pickle的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!