Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In Python 2, <em>type</em> and <em>class</em> are not the same thing, specifically, for old-style classes, <code>type(obj)</code> <a href="https://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python">is not the same object</a> as <code>obj.__class__</code>. So it is possible because instances of old-style classes are actually of a different type (<code>instance</code>) than their class:</p> <pre><code>&gt;&gt;&gt; class A(): pass &gt;&gt;&gt; class B(A): pass &gt;&gt;&gt; b = B() &gt;&gt;&gt; assert b.__class__ is B &gt;&gt;&gt; issubclass(b.__class__, A) # same as issubclass(B, A) True &gt;&gt;&gt; issubclass(type(b), A) False &gt;&gt;&gt; type(b) &lt;type 'instance'&gt; &gt;&gt;&gt; b.__class__ &lt;class __main__.B at 0x10043aa10&gt; </code></pre> <p>This is resolved in new-style classes: </p> <pre><code>&gt;&gt;&gt; class NA(object): pass &gt;&gt;&gt; class NB(NA): pass &gt;&gt;&gt; nb = NB() &gt;&gt;&gt; issubclass(type(nb), NA) True &gt;&gt;&gt; type(nb) &lt;class '__main__.NB'&gt; &gt;&gt;&gt; nb.__class__ &lt;class '__main__.NB'&gt; </code></pre> <p>Old-style class is not a type, new-style class is:</p> <pre><code>&gt;&gt;&gt; isinstance(A, type) False &gt;&gt;&gt; isinstance(NA, type) True </code></pre> <p>Old style classes are declared deprecated. In Python 3, there are only new-style classes; <code>class A()</code> is equivalent to <code>class A(object)</code> and your code will yield <code>True</code> in both checks.</p> <p>Take a look at this question for some more discussion: <a href="https://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python">What is the difference between old style and new style classes in Python?</a></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