Note that there are some explanatory texts on larger screens.

plurals
  1. PO"MetaClass", "__new__", "cls" and "super" - what is the mechanism exactly?
    text
    copied!<p>I have read posts like these:</p> <ol> <li><a href="https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python?</a> </li> <li><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></li> <li><a href="http://fuhm.net/super-harmful/" rel="nofollow noreferrer">Python's Super is nifty, but you can't use it</a></li> </ol> <p>But somehow I got confused. Many confusions like:</p> <p>When and why would I have to do something like the following?</p> <pre><code># Refer link1 return super(MyType, cls).__new__(cls, name, bases, newattrs) </code></pre> <p>or</p> <pre><code># Refer link2 return super(MetaSingleton, cls).__call__(*args, **kw) </code></pre> <p>or</p> <pre><code># Refer link2 return type(self.__name__ + other.__name__, (self, other), {}) </code></pre> <p>How does super work exactly?</p> <p>What is class registry and unregistry in link1 and how exactly does it work? (I thought it has something to do with <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow noreferrer">singleton</a>. I may be wrong, being from C background. My coding style is still a mix of functional and OO).</p> <p>What is the flow of class instantiation (subclass, metaclass, super, type) and method invocation (</p> <pre><code>metaclass-&gt;__new__, metaclass-&gt;__init__, super-&gt;__new__, subclass-&gt;__init__ inherited from metaclass </code></pre> <p>) with well-commented working code (though the first link is quite close, but it does not talk about cls keyword and super(..) and registry). Preferably an example with multiple inheritance.</p> <p>P.S.: I made the last part as code because Stack&nbsp;Overflow formatting was converting the text <code>metaclass-&gt;__new__</code> to metaclass-><strong>new</strong></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