[英]Python class setup for serialization without pickle
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. 我正在寻找一种python中的面向对象的方法,该方法可以将类的实例保存在数据文件中,并在以后的某个时间点再次加载它。 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
and ComplexParam2
are to be calculated parameters and are not used for a first creation of the object A
since they are very complex to get, whereas EasyParam1
and EasyParam2
are "known" parameters. 如您所见, ComplexParam1
和ComplexParam2
是要计算的参数,不用于对象A
的首次创建,因为它们非常复杂,而EasyParam1
和EasyParam2
是“已知”参数。 Think of it as if the EasyParameters
are integers and the ComplexParameters
are large matricies that are constructed based on EasyParameters
可以将其视为EasyParameters
是整数,而ComplexParameters
是基于EasyParameters
构造的大型EasyParameters
So I am using the setup above to Save
and Load
objects to and from file, where Create
uses the constructor since ComplexParam1
and ComplexParam2
are stored in file and do not need to be calculated again. 因此,我使用上面的设置来Save
对象Save
到文件或从文件中Load
对象,其中Create
使用构造函数,因为ComplexParam1
和ComplexParam2
存储在文件中,不需要再次计算。
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. 在C ++中,我将重载构造函数并使该类的两个可能的创建可用,但这在python中不受支持。
Any help, link and suggestion is appreciated. 任何帮助,链接和建议表示赞赏。
I think this is a case for the @classmethod
decorator. 我认为@classmethod
装饰器就是这种情况。 For example, if you change the Load
method to the following: 例如,如果将Load
方法更改为以下内容:
@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 invoke B.__init__
. 最后,您可以调用B.Load(some_file)
,该调用将调用B.__init__
。
Overloading in not needed, just use a classmethod
for alternative contructor methods. 不需要重载,只需将classmethod
用于替代构造方法即可。 Look at this questions and it's answers. 看看这个问题 ,就是答案。
class A (object):
@classmethod
def Load(cls, Filename):
#read ComplexParam1 and ComplexParam2 and call constructor
return cls(ComplexParam1, ComplexParam2)
By using the cls parameter for the class, it works nice with inheritance. 通过为类使用cls参数,它可以很好地继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.