Is subclassing from object the same as defining type as metaclass?
<p>This is an old-style class:</p>

<pre><code>class OldStyle:
    pass
</code></pre>

<p>This is a new-style class:</p>

<pre><code>class NewStyle(object):
    pass
</code></pre>

<p>This is also a new-style class:</p>

<pre><code>class NewStyle2:
    __metaclass__ = type
</code></pre>

<p>Is there any difference whatsoever between <code>NewStyle</code> and <code>NewStyle2</code>?</p>

<p>I have the impression that the only effect of inheriting from <code>object</code> is actually to define the <code>type</code> metaclass, but I cannot find any confirmation of that, other than that I do not see any difference.</p>

<p>Pretty much yes, there's no difference between <code>NewStyle</code> and <code>NewStyle2</code>. Both are of type <code>type</code> while <code>OldStyle</code> of type <code>classobj</code>.</p>

<p>If you subclass from object, the <a href="https://hg.python.org/cpython/file/2.7/Python/ceval.c#l4949" rel="nofollow"><code>__class__</code> of <code>object</code> (meaning <code>type</code>) is going</a> to be used; if you supply a <a href="https://hg.python.org/cpython/file/2.7/Python/ceval.c#l4944" rel="nofollow"><code>__metaclass__</code> that is going to get picked up</a>.</p>

<p>If nothing is supplied as <code>__metaclass__</code> and you don't inherit from <code>object</code>, <a href="https://hg.python.org/cpython/file/2.7/Python/ceval.c#l4961" rel="nofollow"><code>Py_ClassType</code> is assigned as the metaclass</a> for you.</p>

<p>In all cases, <code>metaclass.__new__</code> is going to get called. For <code>Py_ClassType.__new__</code> it follows the semantics defined (I've never examined them, really) and for <a href="https://hg.python.org/cpython/file/2.7/Objects/typeobject.c#l2164" rel="nofollow"><code>type.__new__</code> it makes sure to pack <code>object</code></a> in the bases of your class.</p>

<p>Of course, a similar effect is achieved by:</p>

<pre><code>cls = type("NewStyle3", (), {})
</code></pre>

<p>where a call is immediately made to <code>type</code>; it's just a bigger hassle :-)</p>

python python-2.7 python-internals new-style-class