1

I am interested in saving and load objects using the pickle module as you can read in a question I asked before: Python: Errors saving and loading objects with pickle module

Someone commment:

1, In an other way: the error is raise because pickle wanted to load an instance of the class Fruits and search for the class definition where it was defined, but it didn't find it so it raise the error

Now I want to save and load a class definition in order to solve the problem I describe in the question mentioned before. Thank you so much!

Community
  • 1
  • 1
Peterstone
  • 7,119
  • 14
  • 41
  • 49
  • If you really mean "save and load the class definition", that's what python source files are for... – Andrew Jaffe Jan 01 '11 at 12:35
  • Could you please explain __why__ you want to do that? – Keith Jan 01 '11 at 12:43
  • 2
    Why not just `import` the class, separate from the loading the pickled objects? What's wrong with a simple `import`? – S.Lott Jan 01 '11 at 13:49
  • Putting the class(es) in a separate file and `import` ing them is also what I suggested following your similar last comment/question to my answer to your question [Python: saving and loading objects and using pickle.](http://stackoverflow.com/questions/4530611/python-saving-and-loading-objects-and-using-pickle/4531859#4531859). – martineau Jan 02 '11 at 00:44

1 Answers1

10

The pickle module saves and loads the objects internal state. The code is not a part of the internal state, not even for classes, so therefore it gets tricky.

The obvious way is to make the whole class definition in a string, pickle that string, and then load it, and exec() that string. Another option that may or may not work is to have a metaclass that can pickle and unpickle the code as well, but that is way more difficult, and not really any better.

This is however an extremely bad idea for tons of reasons, and I would bet a significant amount of my reputation point on that you have no good reason to do that. You are with 99.9% likelihood barking up the wrong tree. You are trying to solve a problem you have because you have chosen the wrong solution to do something, and now you are trying to solve the problems that solution is giving you, instead of choosing a better solution that likely will be very simple to implement.

So you need to not only explain the current problem you have, but also the large scale usecase you are trying to solve. We can then tell you how to solve that usecase in a better way.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • +1 for "trying to solve the problems that solution is giving you" - perfect description of this syndrome. – bgporter Jan 01 '11 at 13:38
  • 1
    +1 for the **"This is however an extremely bad idea..."** part. – martineau Jan 02 '11 at 00:47
  • @Lennart Regebro: Some usecase info -- "...what I pursuit is to close a session, open a new one and load what I save in a past session. I close the session after putting the line" filehandler.close()" and I open a new one and I put the rest of your code, then after putting "object_file = pickle.load(file)" I get this error:Traceback (most recent call last): File "", line 1, in object_file = pickle.load(file) File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() AttributeError: 'module' object has no attribute 'Fruits' – Peterstone" – martineau Jan 02 '11 at 00:52
  • @Lennart Regebro: From this and other questions he's asked I suspect what he wants an object database that stores both instances their type (class) definitions. Nothing wrong with that but I doubt he's prepared or understands the high level of abstraction that doing so will involve (nor that he probably doesn't want or need it). – martineau Jan 02 '11 at 01:01
  • @martineu: Ah, when he says "session" he probably means a Python session, not just a session with his software. Yeah, that pretty much can't be done without saving all the python code that was typed. – Lennart Regebro Jan 02 '11 at 07:01
  • @Lennart Regebro: Yes, that is. When I said about close a session I only speaking about to close the interpreter. – Peterstone Jan 04 '11 at 20:02
  • @Peterstone: Why? Anyway, log everything that is written into the interpreter to a file, and load that on interpreter startup. That would work. Not sure how to log it, but still. It seems crazy to me, I have no idea why you'd want that. – Lennart Regebro Jan 04 '11 at 20:41