problem pickling objects created with the type function

danny danny_shevitz at yahoo.com
Wed Apr 21 17:57:17 EDT 2004


Howdy, 

I've run into a problem pickling objects created with a type
statement. Here's the code:

*********************************************
import pickle

class foo(object):
	def __init__(self):
		self.wombat = []
		
	def setWombat(self):
		self.wombat = [1]

# pickle a foo instance		
x = foo()
print pickle.dumps(x) # works

# pickle a foobar instance
childType = type('foobar',(foo,),{'slor':7})
y = childType()
print pickle.dumps(y) # raises an exception
*********************************************

I'm pretty sure the failure is because 'foobar' is never in the global
namespace. If I change the code to foobar = type('foobar',...  then
the code works, but I dont' want to do this because I create the class
name in a factory and it is mangled to prevent different invocations
of the factory from having the same class name.

Does anyone know how to get around this, or how to get 'foobar' =
childType into the global namespace?

thanks,
Danny



More information about the Python-list mailing list