Pickling and unpickling inherited attributes

Steve Holden steve at holdenweb.com
Tue Nov 1 10:19:42 EST 2005


Alex wrote:
> Thanks to both Alex Martelli and Steve Holden.. We need __slots__ for
> other reasons, too long to explain, but basically to prevent assignment
> of extra attributes.
> 
> I ended up changing child classes this way:
> 
>     def __getstate__(self):
>         return(Parent.__getstate__(self), self.C)
>     def __setstate__(self, data):
>         Parent.__setstate__(self, data[0])
>         self.C=data[1:]
> 
> This seems to work fine.
> 
OK, but do be aware that slots was intended solely as a 
memory-conservation measure where large numbers of objects are being 
created. You are therefore subverting its true intent by using it to 
limit the assignment of extra attributes (and there has been much recent 
discussion on this list, though not in this thread, about what to do 
instead.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list