2017-12-14 7 views
0

lmfit으로 객체를 unpickling 문제를 진단되고, 나는 누구의 예를 나는 것 수업 시간에 내 제약 함수 정의를 포함 원했고 나중에 피클. 그러나, 제한 범위 기능이 클래스 범위 내에 정의되어있는 피클은 RuntimeError: maximum recursion depth exceeded (아래 코드의 섹션 (3))을 발생시킵니다.클래스 인스턴스가 유엔 (딜) pickleable이 제한 기능 및 매개 변수는 클래스 범위 내에서 정의 된 경우

나는 이것이어야하는 직관적 인 이유를 생각할 수 없다. 예를 들어 클래스 범위의 OrderedDict에 클래스 범위 함수를 채우면 클래스 인스턴스는 피클 할 수 있습니다 (아래 코드의 섹션 (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) 

답변

1

잘못된 위치에 inside_func을 정의하십시오. __init__에서하십시오.

이 작동 :

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 필요 "정상적인"기능이 될 :

또는
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) 

는, 그것을 정적 방법을 확인하십시오. staticmethod을 사용하지 않으면 Python은 메서드를 인스턴스에 바인딩합니다. 이 바인딩은 재귀를 트리거합니다.