[Tutor] Saving Objects
Andy.cheesman at bristol.ac.uk
Thu Jan 17 15:15:59 CET 2008
That's great and it works too, thank you!
My only arising question is why should i
"derive from object, so that you get a new-style class."
I've looked online and found this
(http://www.geocities.com/foetsch/python/new_style_classes.htm) but I
not sure it tells why I would want to do this this way.
Thanks again for your help
Paul McGuire wrote:
> My initial tests using pickle and a simple class system (shown below) have
> failed. The method shown below fails with a AttributeError:
> 'FakeModule' object has no attribute 'Spod', so when I create a an empty
> class Spod in the new session, it generates an IndexError:(list index out of
> Is there a better way to do this?
> I'm assuming that "# New session" marks the beginning of a separate Python
> file. The problem is that the Spod class definition is not included in the
> pickle file, so you must import that as well, before loading back the
> pickled fish object. Do this;
> 1. Put Spod in its own module, let's call it spod.py. (While you are at it,
> have Spod derive from object, so that you get a new-style class.)
> 2. Create test1.py to pickle a Spod. Have test1.py import spod, and then
> have your code that creates spod.Spod("andy") and pickles it to test.pickle.
> 3. Create test2.py to unpickle a Spod. Have test2.py *also* import spod,
> and then have the rest of your code that follows "# New session". Modify
> the pickle.load statement to save the result to a variable, and then you can
> verify that its name is "andy", type is "Spod", etc.
> -- Paul
> Tutor maillist - Tutor at python.org
More information about the Tutor