What is the purpose of using instance of a class as a super/base class?
<p>What is the purpose of subclassing an instance of a class in Python? Here's an example: </p>

<pre><code>class A:
    def __init__(*args): print(args) 

base = A() 


class Test(base): pass 
</code></pre>

<p>This code works properly under Python, but <code>base</code> is an instance of class <code>A</code> (1) <strong>Why do we need to subclass an instance of a class?</strong> <strong>Is it related to metaclasses?</strong></p>

<p>From this question: 
<a href="http://stackoverflow.com/questions/37876318/what-happens-when-you-inherent-from-a-module-instead-of-a-class-in-python?rq=1">What happens when you inherent from a module instead of a class in Python?</a></p>

<p>I understand that <code>Test(base)</code> will become <code>type(base).__init__</code>, (2) <strong>does this happen at definition time, when the class is defined?</strong> (3) <strong>How does Python know/decide that <code>base</code> is an instance of a class? Is it becuase <code>type(base)</code> doesn't return <code>type</code>?</strong></p>

<p>Python actually uses <code>type(base)(classname, bases, body)</code> to produce the class object. This is the normal metaclass invocation (unless the class specifies a specific metaclass directly).</p>

<p>For an <em>instance</em> (or a module, which is basically an instance too), that means the class <code>__new__</code> static method is called to produce a new instance, and on that new instance <code>__init__</code> is called. So for your <code>class Test(base): pass</code> syntax, you are essentially creating a new instance of <code>A</code>, when Python executes the <code>class</code> statement.</p>

<p>This isn't really 'definition time'; there is no such phase in Python code. Python loads the bytecode for a module and executes it when it is imported for the first time. Class statements at the top of the module (so not inside functions) are executed at that time. It is at that time then that the <code>type(base)(...)</code> call is executed, yes.</p>

<p>Python doesn't 'know' or 'decide' anything about the base class. Python's philosophy is to trust the developer that writes code in it. The assumption is that you know what you are doing, and the base classes are just treated as if they'll respond correctly. Since <code>type(base)(....)</code> didn't raise an exception, Python just continued.</p>

<p>You can use anything that's callable as a metaclass, really.</p>

python class inheritance python-internals