Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You raise a good point. </p> <p>Here is a <a href="http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html" rel="noreferrer">good reference</a>to get a better understanding of the relations between objects, classes and metaclasses:</p> <p>I also find <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="noreferrer">this reference on descriptors</a> to be quite enlightening about the mechanism of look-up in python.</p> <p>But I can't say I understand why <code>a.foo</code> fails when <code>A.foo</code> succeeds. It seems that when you look up an attribute of an object, and python does not find it there, it does <em>not exactly</em> look up the attribute in the class, because if it did, it would find <code>A.foo</code>.</p> <p>EDIT:</p> <p>Oh! I think I got it. It is due to how inheritance works. If you consider the schema provided by the <a href="http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html" rel="noreferrer">above link</a>, it looks like this:</p> <p><a href="http://www.cafepy.com/article/python_types_and_objects/images/types_map.png" rel="noreferrer">alt text http://www.cafepy.com/article/python_types_and_objects/images/types_map.png</a></p> <p>Schematically, it boils down to:</p> <pre><code>type -- object | | Meta -- A -- a </code></pre> <p>Going <em>left</em> means going to the class of a given instance. Going <em>up</em> means going to the <em>parent</em>.</p> <p>Now the inheritance mechanism makes the look-up mechanism make a <em>right turn</em> in the schema above. It goes <code>a → A → object</code>. It must do so in order to follow the inheritance rule! To make it clear, the search path is:</p> <pre><code> object ^ | A &lt;-- a </code></pre> <p>Then, clearly, the attribute <code>foo</code> will not be found.</p> <p>When you lookup for the attribute <code>foo</code> in <code>A</code>, however, it <em>is</em> found, because the lookup path is:</p> <pre><code>type ^ | Meta &lt;-- A </code></pre> <p>It all makes sense when one thinks of how inheritance works.</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