Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Object attribute search mechanism
    primarykey
    data
    text
    <p>As per <a href="http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html" rel="nofollow">this article</a></p> <blockquote> <p>When accessed objectname.attributename, the following objects are searched in sequence for the attribute:</p> </blockquote> <p>1. The object itself (<code>objectname.__dict__</code> or any Python-provided attribute of objectname).</p> <p>2. The object's type (<code>objectname.__class__.__dict__</code>). Observe that only <code>__dict__</code> is searched, which means only user-provided attributes of the class. In other words <code>objectname.__bases__</code> may not return anything even though <code>objectname.__class__.__bases__</code> does exist.</p> <p>3. The bases of the object's class, their bases, and so on. (<code>__dict__</code> of each of <code>objectname.__class__.__bases__</code>). More than one base does not confuse Python, and should not concern us at the moment. The point to note is that all bases are searched until an attribute is found.</p> <p>To test the theory I created this example</p> <pre><code>class Superb(object): svar=1 class Sub(Superb): ... class Leaf(Sub): def __init__(self): print(Leaf.svar) lobj=Leaf() </code></pre> <p>The instance creation worked and printed the value of Leaf.svar (as 1). This means that when resolving Leaf.svar, Python looked at the <em>base of the base of the Leaf object</em>, which is not something mentioned in the article. As per the article, <em>bases of the object's class</em> (i.e. type) are searched . I doubt that the article writer made any mistake it's most certainly a gap in my understanding. Can someone please clarify.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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