Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Each object has a dictionary of attributes that contains it's variables and functions. Referring to these dictionaries:</p> <pre><code>If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence. </code></pre> <p>This is what they were talking about.</p> <p>To show this:</p> <pre><code>#data descriptor class A(object): def __get__(self, obj, type): print "hello from get A" def __set__(self, obj, val): print "hello from set A" #non data descriptor class B(object): def __get__(self, obj, type): print "hello from get B" class C(object): #our data descriptor a = A() #our non data descriptor b = B() &gt;&gt;&gt; c = C() &gt;&gt;&gt; c.a hello from get A &gt;&gt;&gt; c.b hello from get B &gt;&gt;&gt; c.a = 0 hello from set A &gt;&gt;&gt; c.a #notice after reassignment, A.__get__ is still called hello from set A &gt;&gt;&gt; c.b = 0 #instance variable with the same name as the non data-descriptor &gt;&gt;&gt; c.b #notice how B.__get__ isn't called anymore 0 </code></pre> <p>Basically it is saying that when <code>__get__</code> and <code>__set__</code> are user defined for an object (data descriptor), they will be called instead of the default methods. If only <code>__get__</code> is user defined for an object(non-data descriptor), the instance can reassign am instance variable.</p> <p>So when calling <code>g.x = 0</code>: if x is a data descriptor then x's user-defined <code>__set__</code> method is called, when x is an instance variable, or non-data descriptor the default behavior is called.</p> <p>With a <strong>data descriptor</strong>, the class is controlling all access and modification to the variables. <strong>All access to variables of your data descriptor type will go through <code>__get__</code> and <code>__set__</code></strong>. So <code>c.a = 0</code> calls <code>A.__set__</code> and c.a is changed how the class defined it. There is no way to create an instance variable 'c.a' that is not of the type A. </p> <p>With a <strong>non data descriptor</strong>, the class only controls access, so when <code>c.b = 0</code> is called, since <code>__set__</code> isn't defined, a new instance variable is made(the default behavior). There is no user-defined behavior on setting the variable, so you can create an instance variable with the same name, without the <code>__get__</code> behavior.</p> <p>The precedence they are talking about is the dynamics of the two. A data descriptor will always call <code>__get__</code> and <code>__set__</code>, and so an instance variable can't be created with the same name. A non data descriptor will only call <code>__get__</code> until an instance variable with the same name is created.</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