pickling instances of metaclass generated classes

lars van gemerden lars at rational-it.com
Tue Jan 3 06:43:17 EST 2012

On Dec 29 2011, 10:55 am, lars van gemerden <l... at rational-it.com>
> Hello,
> Can someone help me with the following:
> I am using metaclasses to make classes and these classes to make
> instances. Now I want to use multiprocessing, which needs to pickle
> these instances.
> Pickle cannot find the class definitions of the instances. I am trying
> to add a line to the __new__ of the metaclass to add the new class
> under the right name in the right module/place, so pickle can find
> it.
> Is this the right approach? Can anyone explain to me where/how to add
> these classes for pickle to find and maybe why?
> Thanks in advance,
> Lars


After reading all posts (thanks a lot), I am considering to use the
following base metaclass for all metaclasses that must lead to
pickleable instances (not pickleable classes):

import sys

class Meta(type):
    def __new__(mcls, name, bases, attrs):
        cls = type.__new__(mcls, name, bases, attrs)
        setattr(sys.modules[__name__], name, cls)
        return cls

if __name__ == '__main__':
    instance = Meta("Klass", (str,),{})("apple")
    s = pickle.dumps(instance)
    delattr(sys.modules[__name__], "Klass")
    Meta("Klass", (str,),{})
    inst = pickle.loads(s)
    print instance
    print inst
    print type(instance) is type(inst)

Can anyone see any drawbacks to this approach? I've also tested the
case where the Meta metaclass is defined in another module.

Cheers, Lars

More information about the Python-list mailing list