Take the 2-minute tour ×

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.__boardMappings= {boardName:{
                                  {'CPU_A':{'mipi':[], 'gpen':[]},
                                   'CPU_B':{'mipi':[], 'gpen':[]}


    def setMode(self, board, mode, cpu,mipi,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):
        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
TypeError: can't pickle PySwigObject objects

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

share|improve this question
PEP 8 would like you to name your class Board (not BoardClass as you know it's a class), setMode set_mode, etc. Also things like spaces after colons in a dict literal and spaces around the = in assignment. Finally, you should do class Board(object) to make it a new-style class. –  Chris Morgan Apr 25 '12 at 0:07
Your code isn't making sense as it stands. In what class is onSave defined? Where is boardName defined in onSave? Where is the PySwigObject coming from (you're clearly doing more in the board than you've shown)? –  Chris Morgan Apr 25 '12 at 0:08
Chris , it doesn't matter where onSave() is defined its in another class and will be called when user wants to save the board.(or code to pickle it) I don't know where PySwigObject is coming from I don't have that in my code and for pickle part this is all the code. Also doing class Board(object) didn't solve anything. –  user845459 Apr 25 '12 at 1:32
Well, with the code as you've got it, it will work. (Try it.) You've got other significant code somewhere which you haven't shown. My first comment was (almost) purely about stylistic matters; apart from the new-style class bit (which will fix some strange behaviour in obscure places), it won't change any functionality. –  Chris Morgan Apr 25 '12 at 1:33
being pickleable is part of the "strange behaviors" fixed by having a new style class. –  jsbueno Apr 25 '12 at 3:06
show 1 more comment

1 Answer

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):
share|improve this answer
add comment

Your Answer

