How to better pickle an extension type

dgdev dgdev3141 at yahoo.com
Mon Apr 16 17:18:42 CEST 2007


I would like to pickle an extension type (written in pyrex).  I have
it working thus far by defining three methods:

class C:
	# for pickling
	__getstate__(self):
		... # make 'state_obj'
		return state_obj

	__reduce__(self):
		return C,(args,to,__init__),me.__getstate__()

	# for unpickling
	__setstate__(self,state_obj):
		self.x=state_obj.x
		...


This gets the class pickling and unpickling.

However, I'd like to not specify arguments for __init__ (as I do now
in __reduce__), and so not have __init__ invoked during unpickling.

I would like to have the pickling machinery somehow create an
uninitialized object, and then call its __setstate__, where I can re-
create it from 'state_obj'.

Is there a kosher way to do so, that is without me having to have a
special mode in the constructor for when the object is being created
by the unpickler?




More information about the Python-list mailing list