Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the more pragmatic answer.</p> <p><strong>It rarely matters</strong></p> <ol> <li><p>"<a href="https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python</a>". Bottom line, <code>type</code> is the metaclass of all classes. You have almost no practical use for this. </p> <pre><code>class X(object): pass type(X) == type </code></pre></li> <li><p>"<a href="https://stackoverflow.com/questions/392160/what-are-your-concrete-use-cases-for-metaclasses-in-python">What are your (concrete) use cases for metaclasses in Python?</a>". Bottom line. None.</p></li> <li><p>"<a href="http://fuhm.net/super-harmful/" rel="nofollow noreferrer">Python's Super is nifty, but you can't use it</a>". Interesting note, but little practical value. You'll never have a need for resolving complex multiple inheritance networks. It's easy to prevent this problem from arising by using an explicity <strong>Strategy</strong> design instead of multiple inheritance. </p></li> </ol> <p>Here's my experience over the last 7 years of Python programming.</p> <ol> <li><p>A class has 1 or more superclasses forming a simple chain from my class to <code>object</code>.</p></li> <li><p>The concept of "class" is defined by a metaclass named <code>type</code>. I might want to extend the concept of "class", but so far, it's never come up in practice. Not once. <code>type</code> always does the right thing.</p></li> <li><p>Using <code>super</code> works out really well in practice. It allows a subclass to defer to it's superclass. It happens to show up in these metaclass examples because they're extending the built-in metaclass, <code>type</code>. </p> <p>However, in all subclass situations, you'll make use of <code>super</code> to extend a superclass.</p></li> </ol> <p><strong>Metaclasses</strong></p> <p>The metaclass issue is this: </p> <ul> <li><p>Every object has a reference to it's type definition, or "class".</p></li> <li><p>A <code>class</code> is, itself, also an object.</p></li> <li><p>Therefore a object of type <code>class</code> has a reference to it's type or "class". The "class" of a "class" is a metaclass.</p></li> </ul> <p>Since a "class" isn't a C++ run-time object, this doesn't happen in C++. It does happen in Java, Smalltalk and Python.</p> <p>A metaclass defines the behavior of a class object.</p> <ul> <li><p>90% of your interaction with a class is to ask the class to create a new object.</p></li> <li><p>10% of the time, you'll be using class methods or class variables ("static" in C++ or Java parlance.)</p></li> </ul> <p>I have found a few use cases for class-level methods. I have almost no use cases for class variables. I've never had a situation to change the way object construction works.</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