<p>> Ok,<br>
><br>
> After reading all posts (thanks a lot), I am considering to use the<br>
> following base metaclass for all metaclasses that must lead to<br>
> pickleable instances (not pickleable classes):<br>
><br>
><br>
> import sys<br>
><br>
> class Meta(type):<br>
> def __new__(mcls, name, bases, attrs):<br>
> cls = type.__new__(mcls, name, bases, attrs)<br>
> setattr(sys.modules[__name__], name, cls)<br>
> return cls<br>
><br>
><br>
> if __name__ == '__main__':<br>
> instance = Meta("Klass", (str,),{})("apple")<br>
> s = pickle.dumps(instance)<br>
> delattr(sys.modules[__name__], "Klass")<br>
> Meta("Klass", (str,),{})<br>
> inst = pickle.loads(s)<br>
> print instance<br>
> print inst<br>
> print type(instance) is type(inst)<br>
><br>
> Can anyone see any drawbacks to this approach? I've also tested the<br>
> case where the Meta metaclass is defined in another module.</p>
<p>With this approach you'll need to be conscious of what names you allow for classes. If you create any classes named "Meta" or "sys", or that shadow any of the builtins, things could break. For that reason I think the __reduce__ approach is cleaner (plus it works with the pickle system instead of trying to fool it, so it may be more robust ib the long term), but it's your project.</p>