Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can change the metaclass after class creation the same way that you can change the class of an object, however you'd have a lot of issues. For starters, the initial metaclass needs to be different from <code>type</code>, the <code>__init__</code> and <code>__new__</code> of the new metaclass won't be called (though you can manually call <code>__init__</code> or a method that performs <code>__init__</code>'s job).</p> <p>Possibly the least troublesome way to change the metaclass is to recreate the class again from scratch:</p> <pre><code>B = MetaClass(B.__name__, B.__bases__, B.__dict__) </code></pre> <p>But if you insist on changing the metaclass dynamically, you first need to define B with a temporary custom metaclass:</p> <pre><code>class _TempMetaclass(type): pass class B: __metaclass__ = _TempMetaclass # or: type('temp', (type, ), {}) </code></pre> <p>Then you can define the metaclass like that:</p> <pre><code>class MetaClass(type): def __init__(cls, *a, **kw): super(MetaClass, cls).__init__(*a, **kw) cls._actual_init(*a, **kw) def _actual_init(cls, *a, **kw): # actual initialization goes here </code></pre> <p>And then do something like:</p> <pre><code>B.__class__ = MetaClass MetaClass._actual_init(B, B.__name__, B.__bases__, B.__dict__) </code></pre> <p>You also need to make sure that all the initialization of the class is done <code>_actual_init</code>. You can also add a <code>classmethod</code> to the metaclass that changes the metaclass for you.</p> <p>Both solutions have the slight shortcoming that B's bases would be limited - they need to be compatible with both the original and the new metaclass, but I guess that's not an issue in your case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload