2

Apologies if this is already answered elsewhere. I have looked around the internet and didn't find a clear answer

I have a class definition (that contains several values and methods), and multiple instantiations of the class held in a list. (Each list entry is an instantiation.)

When I try to pickle the list I get a "pickle.PicklingError" exception. This led me to learn that some objects are not 'pickleable', but it seems like my simple list should be OK.

Which objects are unpickleable?

Here is the actual code doing the pickling. (this code is a method defined inside of a class which also contains the class objects I need to pickle. Is this part of the problem?)

def Write_Transaction_History_To_File(self):
    if (self.Transaction_History == True): # if History is not empty

        filename = self.Transaction_Name + '_Transaction_History.bin'
        f = open(filename, 'w')

        try:
            pickle.dump(self.Transaction_History , f, -1)   #use highest protocol
        except pickle.PicklingError:
            print 'Error when serializing data'
        f.close()

    else:
        print 'No History to store'
gearhead
  • 787
  • 1
  • 6
  • 26

1 Answers1

3

If you try to pickle nested classes not in the module scope, you will run into trouble.

From the docs:

The following types can be pickled:


None, True, and False
integers, long integers, floating point numbers, complex numbers
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose __dict__ or the result of calling __getstate__() is 
picklable (see section The pickle protocol for details).
root
  • 76,608
  • 25
  • 108
  • 120
  • Hmm. This may be the problem. All the the relevant things here are attributes of a class, call it class A. Class A contains the definition of another class, Class B (that is only ever used here). The object to be pickled is an attribute of Class A, its a list of instantiations of Class B. The function doing the pickling is a method in Class A. I can't tell, does this situation constitute "nested classes not in the module scope"? Thanks for your help. – gearhead Jan 07 '13 at 22:12
  • If the class contains the defines another class, then this is a problem,yes. – root Jan 07 '13 at 22:17
  • also read http://stackoverflow.com/questions/1947904/how-can-i-pickle-a-nested-class-in-python ,there are some hackish workarounds -- best soultion probaly to refactor your classes though. – root Jan 07 '13 at 22:22
  • This issue is almost certainly the cause of my problem. It will be a pain, but I'd rather refactor the code than use a work-around hack. Thanks. – gearhead Jan 07 '13 at 22:45