[Tutor] problem with unpickling an object

Kent Johnson kent37 at tds.net
Mon Jun 5 16:25:36 CEST 2006


Yi Qiang wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Kent Johnson wrote, On 06/05/2006 03:12 AM:
>>  From a quick read of the docs, it looks like you should set the 
>> unpickler's find_global attribute to a function you define. This 
>> function can first attempt to load the desired class (see 
>> pickle.find_class() for an example). If the load fails, then return a 
>> placeholder class that you define.
>>
>> This won't give exactly the result you ask for, instead of None in the 
>> dictionary you will have instances of the placeholder. You could then 
>> post-process the dict to change the placeholder instances to None if you 
>> like.
> 
> Interesting, so if I used this method I won't be able to use cPickle
> anymore.  The documentation just says that cPickle is "faster", but can
> anyone quantify how much speed I am losing if I move to the regular
> pickle module?

No, the find_global() hook is for cPickle. See the next-to-last 
paragraph on this page:
http://docs.python.org/lib/pickle-sub.html

As far as speed, the best thing is just to try it with your data. pickle 
and cPickle have the same API so it should be easy to switch your code 
to use pickle and run a timing test.

Kent



More information about the Tutor mailing list