0

为了诊断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)
4

1 回答 1

1

你定义inside_func在错误的地方。在__init__.

这有效:

class test(object):

    def __init__(self):

        def inside_func(*args):
            return

        # 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': inside_func})
        return

t = test()

with open('test.pkl', 'wb') as f:
    dill.dump(t, f)

或者,将其设为静态方法:

class test(object):

    @staticmethod
    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)

self.inside_func需要是“正常”功能。如果不使用staticmethod,Python 会将方法绑定到实例。此绑定触发递归。

于 2017-12-14T15:42:18.907 回答