Pickling Python New Style Object Using State

Pickling is basically Python object serialization.

See the Python PEP 307 Extensions to the pickle protocol for further details.

Below, I show a simple way to pickle a new-style object using the state of the object. obj.__setstate__(state) will be called when unpickling if the state is being pickled.

def __newobj__(cls, *args): return cls.__new__(cls, *args) # NEWOBJ pickle

class AClass(object):


def __reduce__(self): return (__newobj__, (self.__class__,), self.__getstate__())

def __getstate__(self):

state = .. # object's data

return state

def __setstate__(self, st):

# restore object's data

The new protocol 2 pickles are much smaller than classic pickles. (due to the __newobj__ unpickling function - new opcode )

When the unpickling function returned by __reduce__ (the first

item of the returned tuple) has the name __newobj__, something

special happens for pickle protocol 2. An unpickling function

named __newobj__ is assumed to have the following semantics:

def __newobj__(cls, *args):

return cls.__new__(cls, *args)

Pickle protocol 2 special-cases an unpickling function with this

name, and emits a pickling opcode that, given 'cls' and 'args',

will return cls.__new__(cls, *args) without also pickling a

reference to __newobj__ (this is the same pickling opcode used by

protocol 2 for a new-style class instance when no __reduce__

implementation exists). This is the main reason why protocol 2

pickles are much smaller than classic pickles.


blog comments powered by Disqus