Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are subtle differences, mostly relating to inheritance. When using a function as a metaclass, the resulting class is really an instance of <code>type</code>, and can be inherited from without restriction; however, the metaclass function will never be called for such subclasses. When using a subclass of <code>type</code> as a metaclass, the resulting class will be an instance of that metaclass, as will any of its subclasses; however, multiple inheritance will be restricted.</p> <p>Illustrating the differences:</p> <pre><code>&gt;&gt;&gt; def m1(name, bases, atts): &gt;&gt;&gt; print "m1 called for " + name &gt;&gt;&gt; return type(name, bases, atts) &gt;&gt;&gt; &gt;&gt;&gt; def m2(name, bases, atts): &gt;&gt;&gt; print "m2 called for " + name &gt;&gt;&gt; return type(name, bases, atts) &gt;&gt;&gt; &gt;&gt;&gt; class c1(object): &gt;&gt;&gt; __metaclass__ = m1 m1 called for c1 &gt;&gt;&gt; type(c1) &lt;type 'type'&gt; &gt;&gt;&gt; class sub1(c1): &gt;&gt;&gt; pass &gt;&gt;&gt; type(sub1) &lt;type 'type'&gt; &gt;&gt;&gt; class c2(object): &gt;&gt;&gt; __metaclass__ = m2 m2 called for c2 &gt;&gt;&gt; class sub2(c1, c2): &gt;&gt;&gt; pass &gt;&gt;&gt; type(sub2) &lt;type 'type'&gt; </code></pre> <p>Note that when defining sub1 and sub2, no metaclass functions were called. They will be created exactly as if c1 and c2 had no metaclasses, but instead had been manipulated after creation.</p> <pre><code>&gt;&gt;&gt; class M1(type): &gt;&gt;&gt; def __new__(meta, name, bases, atts): &gt;&gt;&gt; print "M1 called for " + name &gt;&gt;&gt; return super(M1, meta).__new__(meta, name, bases, atts) &gt;&gt;&gt; class C1(object): &gt;&gt;&gt; __metaclass__ = M1 M1 called for C1 &gt;&gt;&gt; type(C1) &lt;class '__main__.M1'&gt; &gt;&gt;&gt; class Sub1(C1): &gt;&gt;&gt; pass M1 called for Sub1 &gt;&gt;&gt; type(Sub1) &lt;class '__main__.M1'&gt; </code></pre> <p>Note the differences already: M1 was called when creating Sub1, and both classes are instances of M1. I'm using <code>super()</code> for the actual creation here, for reasons which will become clear later.</p> <pre><code>&gt;&gt;&gt; class M2(type): &gt;&gt;&gt; def __new__(meta, name, bases, atts): &gt;&gt;&gt; print "M2 called for " + name &gt;&gt;&gt; return super(M2, meta).__new__(meta, name, bases, atts) &gt;&gt;&gt; class C2(object): &gt;&gt;&gt; __metaclass__ = M2 M2 called for C2 &gt;&gt;&gt; type(C2) &lt;class '__main__.M2'&gt; &gt;&gt;&gt; class Sub2(C1, C2): &gt;&gt;&gt; pass M1 called for Sub2 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "&lt;stdin&gt;", line 23, in __new__ TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases </code></pre> <p>This is the major restriction on multiple inheritance with metaclasses. Python doesn't know whether M1 and M2 are compatible metaclasses, so it forces you to create a new one to guarantee that it does what you need.</p> <pre><code>&gt;&gt;&gt; class M3(M1, M2): &gt;&gt;&gt; def __new__(meta, name, bases, atts): &gt;&gt;&gt; print "M3 called for " + name &gt;&gt;&gt; return super(M3, meta).__new__(meta, name, bases, atts) &gt;&gt;&gt; class C3(C1, C2): &gt;&gt;&gt; __metaclass__ = M3 M3 called for C3 M1 called for C3 M2 called for C3 &gt;&gt;&gt; type(C3) &lt;class '__main__.M3'&gt; </code></pre> <p>This is why I used <code>super()</code> in the metaclass <code>__new__</code> functions: so each one can call the next one in the MRO.</p> <p>Certain use cases might need your classes to be of type <code>type</code>, or might want to avoid the inheritance issues, in which case a metaclass function is probably the way to go. In other cases, the type of the class might be truly important, or you might want to operate on all subclasses, in which case subclassing <code>type</code> would be a better idea. Feel free to use the style that fits best in any given situation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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