gpt4 book ai didi

python - 如何 pickle 继承自 A 的类 B(具有许多变量)的对象,该对象定义了 __setstate__ 和 __getstate__

转载 作者:太空狗 更新时间:2023-10-30 03:06:21 27 4
gpt4 key购买 nike

我的问题是:

class A(object):
def __init__(self):
#init
def __setstate__(self,state):
#A __setstate__ code here
def __getstate__(self):
#A __getstate__ code here
return state

class B(A):
def __init__(self):
#creates many object variables here

A 来自外部图书馆。

硬解

我想避免这种情况

当 pickle B 时,pickle 当然使用类 A 的 __setstate__, __getstate__ 方法,所以为了 pickle 工作我应该做这样的事情:

class B(A):
def __init__(self):
#creates many object variables here

def __setstate__(self,state)
A.__setstate__(self,state)
#B __setstate__ code here
#getting various variables from state for example
self._a0 = state['a0']
self._a1 = state['a1']
#...
self._a100 = state['a100']
self._a101 = state['a101']

def __getstate__(self):
state = A.__getstate__(self)
#B __setstate__ code here
#filling state with various variables
#getting various variables from state for example
state['a0'] = self._a0
state['a1'] = self._a1
#...
state['a100'] = self._a100
state['a101'] = self._a101
return state

我的问题是:

如何避免在 B 中定义 __setstate____getstate__ 以便 pickle 自己完成 pickle 变量的工作?B 中的所有变量都是 pickle 可以自行 pickle(handle) 的类型。因此,如果 B 不继承自 A,则可能会产生良好的结果:

b = B()
path = 'path.temp'
fout = open(path,'w')
pickler = pickl.Pickler(fout)

pickler.dump(b)
fout.close()

fin = open(path,'r')
upickler = pickl.Unpickler(fin)
b = unpickler.load()
fin.close()
#b has all variables

显而易见的解决方案

class B(object):
def __init__(self):
#creates many object variables here
a = A()

但是我希望 B 继承自 A。知道如何解决这个问题,或者至少在 B 中自动化 pickling/unpickling 变量吗?

解决方法:

至于硬解中的自动化 pickle

向 B 添加一个包含变量的字典以 pickle:

class B(A):
__picklableObjects__ = {'_a0', '_a1', ... ,'_a101'}

def __init__(self):
#creates many object variables here
A.__init__(self)
self._a0 = ...
...
self._a101 = ...

@staticmethod
def getPicklableObjects():
return B.__picklableObjects__

def __setstate__(self,state):
A.__setstate__(self,state)
for po in B.getPicklableObjects():
__dict__[po] = state[po]

def __getstate__(self):
state = A.__getstate__(self)
for po in B.getPicklableObjects():
state[po] = copy.deepcopy(__dict__[po])
return state

还有其他想法吗?

A的图书馆:

好的,对于任何感兴趣的人来说,A 是 graph_tool.Graph: A src code

line 786: class Graph(object)

...

line 1517: __getstate__

...

line 1533: __setstate__

最佳答案

根据文档,当未定义 __getstate__ 时,实例的 __dict__ 会被 pickle,因此您可以使用它来定义自己的状态方法作为组合A 方法和实例的 __dict__:

import pickle

class A(object):
def __init__(self):
self.a = 'A state'

def __getstate__(self):
return {'a': self.a}

def __setstate__(self, state):
self.a = state['a']

class B(A):
def __init__(self):
A.__init__(self)
self.b = 'B state'

def __getstate__(self):
a_state = A.__getstate__(self)
b_state = self.__dict__
return (a_state, b_state)

def __setstate__(self, state):
a_state, b_state = state
self.__dict__ = b_state
A.__setstate__(self, a_state)

b = pickle.loads(pickle.dumps(B()))
print b.a
print b.b

关于python - 如何 pickle 继承自 A 的类 B(具有许多变量)的对象,该对象定义了 __setstate__ 和 __getstate__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8574742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com