unpickling Set as set
George Sakkis
george.sakkis at gmail.com
Wed Nov 8 03:26:30 EST 2006
Nick Vatamaniuc wrote:
> The two are not of the same type:
>
> ---------------------------------
> In : import sets
> In : s1=sets.Set([1,2,3])
>
> In : s2=set([1,2,3])
>
> In: type(s1)
> Out: <class 'sets.Set'>
>
> In : type(s2)
> Out: <type 'set'>
>
> In : s1==s2
> Out: False # oops!
>
> In: s2==set(s1)
> Out: True # aha!
> ----------------------------------
>
> You'll have to just cast:
> unpickled_set=set(unpickled_set)
>
> -Nick V.
Or you may have this done automatically by hacking the Set class:
from sets import Set
import cPickle as pickle
Set.__reduce__ = lambda self: (set, (self._data,))
s = Set([1,2,3])
x = pickle.dumps(s)
print pickle.loads(x)
This doesn't work though if you have already pickled the Set before
replacing its __reduce__, so it may not necessarily be what you want.
If there is a way around it, I'd like to know it.
George
More information about the Python-list
mailing list