[issue3816] __newobj__ pickle feature is not documented
Christian Heimes
report at bugs.python.org
Thu Oct 30 00:40:01 CET 2008
Christian Heimes <lists at cheimes.de> added the comment:
Christian Heimes wrote:
> Christian Heimes <lists at cheimes.de> added the comment:
>
> Alexandre Vassalotti wrote:
>> Alexandre Vassalotti <alexandre at peadrop.com> added the comment:
>>
>> Could explain me how this feature could be used, other than for
>> providing the efficient and backward-compatible pickling mechanism for
>> new-style classes?
>
> The feature makes it easy to write __reduce__ methods for subclasses of
> builtins. Take this example:
>
> def __newobj__(cls, *args):
> return cls.__new__(cls, *args)
>
> class mydict(dict):
> def __reduce__(self):
> state = (dict(self), self.__dict__)
> return (__newobj__, (self.__class__,), state)
Of course one has to provide a __setstate__ method, too.
def __setstate__(self, state):
dict.update(self, state[0])
self.__dict__.update(state[1])
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3816>
_______________________________________
More information about the Python-bugs-list
mailing list