Pickle question

Gonçalo Rodrigues op73418 at mail.telepac.pt
Fri Oct 10 14:57:51 EDT 2003


Hi,

Can I force the pickling/unpickling mechanism to call the class
constructor? I thought __getinitargs__ would do the job, but the
following shows it does not.

>>> class Test(object):
	def __init__(self, **kwargs):
		super(Test, self).__init__(**kwargs)
		self.__dict__.update(kwargs)
	def __getinitargs__(self):
		print "Calling __getinitargs__."
		return ()
	def __getattr__(self, attrib):
		print "Getting %s." % attrib
		raise AttributeError

	
>>> a = Test(name = None)
>>> s = pickle.dumps(a)
Getting __getstate__.
Getting __slots__.
>>> b = pickle.loads(s)
Getting __setstate__.
>>> 

I thought unpickling with __getinitargs__ present would go as:

1. Call class constructor with args read from pickle and return inst.
2. Update inst by either calling __setstate__ (if there is) or just
update inst.__dict__.

What am I missing?

With my best regards,
G. Rodrigues




More information about the Python-list mailing list