为了诊断lmfit对象的dill unpickling 问题,我想尝试将我的约束函数定义包含在我稍后将 pickle 其实例的类中。但是,在类范围内定义约束函数的地方转储一个莳萝泡菜会引发(下面代码的第 (3) 节)。RuntimeError: maximum recursion depth exceeded
我想不出为什么会这样的直观原因。例如,如果我将一个类作用域函数填充到一个类作用域 OrderedDict 中,则该类实例仍然是dill可腌制的(下面代码的第 (1) 节)。
熟悉lmfit的人:知道为什么lmfit / asteval会触发这种行为吗?
那些熟悉 OOP 但不熟悉lmfit的人:对可能导致此类问题的代码类型有什么想法吗?
谢谢!
import dill
import lmfit
from collections import OrderedDict as od
# Python 2.7.13; dill 0.2.7.1; lmfit 0.9.7; numpy 1.13.3; scipy 1.0.0
#%% 1) this works ####################################
class test(object):
def inside_func(*args):
return
def __init__(self):
# check OrderedDict dill-pickleable (should be)
self.d = od([])
self.d.update({'func': self.inside_func})
return
t = test()
with open('test.pkl', 'wb') as f:
dill.dump(t, f)
#%% 2) this also works ###############################
def outside_func(*args):
return
class test(object):
def __init__(self):
# some dummy Parameters set
self.p = lmfit.Parameters()
self.p.add('var1')
self.p.add('var2')
# addition of 'func' to _asteval's symtable from outside class scope
self.p._asteval.symtable['func'] = outside_func
return
t = test()
with open('test.pkl', 'wb') as f:
dill.dump(t, f)
#%% 3) this doesn't work ###############################
class test(object):
def inside_func(*args):
return
def __init__(self):
# some dummy Parmaeters set
self.p = lmfit.Parameters()
self.p.add('var1')
self.p.add('var2')
# addition of 'func' to _asteval's symtable from inside class scope
if not any('func' == x for x in self.p._asteval.symtable.keys()):
self.p._asteval.symtable.update({'func': self.inside_func})
return
t = test()
with open('test.pkl', 'wb') as f:
dill.dump(t, f)