Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Alright, it's the usual "<code>super()</code> cannot be used with an old-style class".</p> <p>However, the important point is that <strong>the correct test</strong> for "is this a new-style <strong>instance</strong> (i.e. object)?" is</p> <pre><code>&gt;&gt;&gt; class OldStyle: pass &gt;&gt;&gt; instance = OldStyle() &gt;&gt;&gt; issubclass(instance.__class__, object) False </code></pre> <p>and not (as in the question):</p> <pre><code>&gt;&gt;&gt; isinstance(instance, object) True </code></pre> <p>For <strong>classes</strong>, the correct "is this a new-style class" test is:</p> <pre><code>&gt;&gt;&gt; issubclass(OldStyle, object) # OldStyle is not a new-style class False &gt;&gt;&gt; issubclass(int, object) # int is a new-style class True </code></pre> <p>The <strong>crucial point</strong> is that with old-style classes, the <em>class</em> of an instance and its <em>type</em> are distinct. Here, <code>OldStyle().__class__</code> is <code>OldStyle</code>, which does not inherit from <code>object</code>, while <code>type(OldStyle())</code> is the <code>instance</code> type, which <em>does</em> inherit from <code>object</code>. Basically, an old-style class just creates objects of type <code>instance</code> (whereas a new-style class creates objects whose type is the class itself). This is probably why the instance <code>OldStyle()</code> is an <code>object</code>: its <code>type()</code> inherits from <code>object</code> (the fact that its class does <em>not</em> inherit from <code>object</code> does not count: old-style classes merely construct new objects of type <code>instance</code>). Partial reference: <a href="https://stackoverflow.com/a/9699961/42973">https://stackoverflow.com/a/9699961/42973</a>.</p> <p>PS: The difference between a new-style class and an old-style one can also be seen with:</p> <pre><code>&gt;&gt;&gt; type(OldStyle) # OldStyle creates objects but is not itself a type classobj &gt;&gt;&gt; isinstance(OldStyle, type) False &gt;&gt;&gt; type(int) # A new-style class is a type type </code></pre> <p>(old-style classes are <em>not</em> types, so they cannot be the type of their instances).</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