[Tutor] Pickling objects and then carrying on development
Adam
adam at monkeez.org
Sat Jul 3 02:02:11 EDT 2004
Tim Peters wrote:
> [Adam]
>
>>OK - I have a kind of strange question. I'm developing an
>>application which acts as a catalogue for magazine articles,
>>storing each piece of information in an OO manner. I've
>>written those objects to disk through pickle, which is an
>>enormously cool feature of python.
>>
>>My question relates to the pickled object and also the code
>>that relates to those objects. If I enter data in and pickle
>>this data, and then later develop my code adding more
>>methods and functionality to each class, will the old data
>>automatically gain all this new functionality when it is
>>unpickled later?
>
>
> Yes, it will. pickle stores only data, never code. This isn't a
> necessary behavior, it's just how things are. The code that makes up
> your class is not pickled. Instead, when an instance of your class is
> pickled, the *path* to your class (its module and class name) is
> pickled, as a string. Unpickling effectively imports the (current)
> class code via the path in that string.
>
> Note that this may not always be what you want, though. For example,
> if you rename your class, or move your class into a different module
> or package, then you'll no longer be able to load old pickles
> referencing that class (using its former name or location).
>
>
>>The sequence would be:
>>1) Data entry and pickle...
>>2) Develop and improve code
>>3) Load data
>>4) continue with data entry
>>5) pickle once more
>>
>>Does the new pickle pick up all the new code that I've been
>>working on,
>
>
> Yes, but because the code was never in the pickle, just the data, and
> a string recording from where to import the class.
Many thanks Peter - makes perfect sense.
adam
More information about the Tutor
mailing list