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!

4
  • If you really mean "save and load the class definition", that's what python source files are for... Commented Jan 1, 2011 at 12:35
  • Could you please explain why you want to do that? Commented Jan 1, 2011 at 12:43
  • 2
    Why not just import the class, separate from the loading the pickled objects? What's wrong with a simple import? Commented Jan 1, 2011 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.. Commented Jan 2, 2011 at 0:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 for "trying to solve the problems that solution is giving you" - perfect description of this syndrome.
+1 for the "This is however an extremely bad idea..." part.
@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 "<pyshell#5>", line 1, in <module> 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"
@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).
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.