Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>class Test(object): i = 0 def add(self): Test.i += 1 print "Test.i == ",Test.i print "'i' in dir(Test) == ",'i' in dir(Test) print "'i' in Test.__dict__ == ",'i' in Test.__dict__ t1 = Test() print '\n# t1 = Test() executed' print "t1.i == ",t1.i print "'i' in dir(t1) == ",'i' in dir(t1) print "'i' in t1.__dict__ == ",'i' in t1.__dict__ </code></pre> <p>result</p> <pre><code>Test.i == 0 'i' in dir(Test) == True 'i' in Test.__dict__ == True # t1 = Test() executed t1.i == 0 'i' in dir(t1) == True 'i' in t1.__dict__ == False </code></pre> <p>.</p> <p>The attribute of name <code>__dict__</code> of an object is the dictionary that represents the namespace of this object, that is to say it contains the attributes of the object.</p> <p>Also, we have:</p> <blockquote> <p>dir([object])</p> <p>If the object has a method named <code>__dir__()</code>, this method will be called and must return the list of attributes.</p> </blockquote> <p>So, how is it possible that <code>dir(t1)</code> contains the attribute <strong>i</strong> and <code>t1.__dict__</code> doesn't ? While all two <code>dir(Test)</code> and <code>Test.__dict__</code> contain the attribute <strong>i</strong> ?</p> <p>.</p> <p>That's for the same reason that answers to your question: </p> <p>The behavior of returning the attribute of an object is awkward.<br> And <code>`dir(object)</code>reflects this awkwardness:</p> <blockquote> <p>The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:</p> <p>•If the object is a module object, the list contains the names of the module’s attributes.<br> •If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.<br> •Otherwise, the list contains <em>the object’s attributes’ names</em> , <strong>the names of its class’s attributes</strong>, and recursively of the attributes of its class’s base classes.</p> </blockquote> <p>.</p> <p>Then, the answer to your question is in two parts:</p> <p>1) <code>dir(t1)</code> gives what is considered as the attributes of the object but the REAL NAMESPACE of the object, exposed as <code>t1.__dict__</code> doesn't contain the attribute <strong>i</strong>, which is, as it has already been answered, in fact, a class' atribute. </p> <p>Hence the pseudo-attributes <strong>i</strong> of two instances <strong>t1</strong> and <strong>t2</strong> appear equals because the expressions <code>t1.i</code> and <code>t2.i</code> are in fact evaluated as <code>Test.i</code>,<br> in the same way that <code>dir(t1)</code> and <code>dir(t2)</code> include the class's attribute <code>Test.i</code> .</p> <p>2) how the expression <code>t1.i</code> is evaluated as <code>Test.i</code> ??<br> Because of that (which constitutes the additional info of my answer):</p> <blockquote> <p>A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. </p> <p><a href="http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy" rel="nofollow">http://docs.python.org/reference/datamodel.html#the-standard-type-hierarchy</a></p> </blockquote> <p>It means that when an attribute isn't found in the namespace of an object, it is searched in the namespace of the class whose the object is an instance.</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.
    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