Note that there are some explanatory texts on larger screens.

plurals
  1. PObehaviour of descriptor concept in python (confusing)
    primarykey
    data
    text
    <p>I understood python descriptor but I have a little confusion about this..</p> <p>if you have a class descriptor as follows</p> <pre><code>class Descriptor(object): def __get__(self, instance, owner): print 'getting' return self.value def __set__(self, instance, value): print 'setting' self.value = value def __delete__(self, instance): print 'deleting' del self.value </code></pre> <p>and a class whose attributes we want to manage is something like this..</p> <pre><code>class Test(object): name = Descriptor() def __init__(self, name): print 'init test' self.name = name </code></pre> <p>when I create object of class Test and do something it gives me answer like this...</p> <pre><code>t = Test('abc') init test setting &gt;&gt;&gt; t.name getting 'abc' &gt;&gt;&gt; del t.name deleting &gt;&gt;&gt; t &lt;__main__.Test object at 0x013FCCD0&gt; &gt;&gt;&gt; t.name getting </code></pre> <p>Now I want to have a class Test1 something like this..</p> <pre><code>class Test1(object): def __init__(self, value): print 'init test1' self.name = Descriptor() self. value = value </code></pre> <p>and if I create object of Test1 and try to access attribute of instance of Test1, I get output something like this..</p> <pre><code>t1 = Test1(12) t1.name &gt;&gt;&gt; getting &gt;&gt;&gt; 12 &gt;&gt;&gt; t1.name = 30 &gt;&gt;&gt; setting </code></pre> <p>Q 1) my question is that is this name attribute declared in init of Test1, is bound to instance of Test1 or not... because when I try to get attribute dictionary of t1, it return empty dict...</p> <pre><code>t1.__dict__ &gt;&gt;&gt; {} </code></pre> <p>same for class Test's instance t</p> <pre><code> t.__dict__ &gt;&gt;&gt; {} </code></pre> <p>When I add a new attribute to any of these instances, like this... </p> <pre><code> t.some = 'some' &gt;&gt;&gt; t1.some = 'some' </code></pre> <p>and again if I try to access attribute dictionary it gives me only which I have added just now.. now all instance attribute </p> <pre><code>t.__dict__ &gt;&gt;&gt; {'some': 'some'} &gt;&gt;&gt; t1.__dict__ &gt;&gt;&gt; {'some': 'some'} </code></pre> <p>Q 2) So what is the difference between instance attributes defined in init (like variable name and value in class Descriptor and Test) and attributes defined after instance creation (like variable t.some).</p> <p>Q 3) How class Test is different than class Test1. </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