Pickling slotted instances

Martin v. Löwis loewis at informatik.hu-berlin.de
Thu Sep 12 06:12:45 EDT 2002


Andrew McNamara <andrewm at object-craft.com.au> writes:

> Instances using __slots__ can't be pickled - is there a fundemental reason
> why this is so, or is it simply yet to be implemented?

See python.org/sf/520644 for a longer discussion.

> At first glance, a __getstate__ for a slotted class could be as simple as:
> 
>     def __getstate__(self):
>         attrs = [getattr(self, attr, None) for attr in self.__slots__]
>         return self.__slots__, attrs
> 
> What am I missing?

That won't work: it won't access the slots of the base type. Also, you
are missing a __setstate__ implementation.

Regards,
Martin



More information about the Python-list mailing list