简体   繁体   中英

pickle python Class instance

I have a simple class to just store data that's associated to a circuit board like this :

class boardClass():

    def __init__(self,boardName):
        self.__name=boardName
        self.__boardMappings= {boardName:{
                                  'FastMode':
                                  {'CPU_A':{'mipi':[], 'gpen':[]},
                                   'CPU_B':{'mipi':[], 'gpen':[]}

                                  'SlowMode':
                                  {'CPU_A':{'mipi':[], 'gpen':[]},
                                   'CPU_B':{'mipi':[], 'gpen':[]}                                   
                                  }
                                 }                       
                               }


    def setMode(self, board, mode, cpu,mipi,gpen):
        self.__boardMappings[board][mode][cpu]['mipi']=mipi
        self.__boardMappings[board][mode][cpu]['gpen']=gpen

    def getName(self):
        return self.__name

I use pickle in another class to store the boardClass data in file and later read them:

def onSave(self,boardName):
        board=boardClass.boardClass(boardName)
        name=boardName+".brd"
        file=open(name,"wb")            
        pickle.dump(board,file)                        
        loadedBoard= pickle.load( open( file, "rb" ))            
        print "Loaded board name is : ",loadedBoard.getName()

When I call onSave() method to pickle the boardClass, it gives several errors ending with this at end:

File "C:\Python27\lib\copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle PySwigObject objects

This boardClass is very simple container. Why can't it be pickled?

Instead of inherting from "nothing", inherit from "object" - this way your class will no longer be a "Class Instance" - it will be a proper instance of a new-style class, and as such, be "pickeable"

In other words, just try changing this line:

class boardClass():

to this:

class boardClass(object):

update : This answer is from Python 2 era - and explicitly inheritng from object was needed there. In Python 3 it is no longer the case: inheritance from object is automatic, and if pickle is failing it is for some other reason.

如果你真的想保留你的对象,这样它就不会在参数中使用“对象”,你可以使用像dillcloudpickle这样的序列化cloudpickle ,它可以序列化旧式和新式类实例(对于 python 2.x 和3.x 语法)。

您不能腌制PySwigObjects ,但是这里有一个解决方法:腌制对象

This is an old question, and the name is quite generic, so if you see it now you probably want to set __getstate__ and __setstate__ of your class so pickle would know how to dump and load you defined class.

See examples here.

If your class is simple (eg only have ints and strings as members and any method) it should be pickalable automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM