Pickling of dynamic class definition
问题 I am trying to pickle a dynamically generated class as a factory for an alternative class. Something like the following: import sys, pickle class BC(object): pass C = type("NewClassName", (BC,), {}) pickle.dump(C, sys.stdout) This leads to the following error: pickle.PicklingError: Can't pickle <class '__main__.NewClassName'>: it's not found as __main__.NewClassName For pickling an object of a dynamically generated class, you can define an __reduce__ method, but is there a way to achieve this