How can I use descriptors for non-static methods?
<p>I am aware that I can use descriptors to change static property as if it were a normal property. However, when I try using descriptors for a normal class property, I end up changing the object it references to, instead of the value in the object.</p>

<p>If I have</p>

<p>Normal use, considering that the method(param) returns an object</p>

<pre><code>class SomeClass():
    property = method(param)
</code></pre>

<p>I can then do:</p>

<pre><code>instance = SomeClass()
instance.property = 3
</code></pre>

<p>and be able to have that setting handled by the the class of which property is an instance.</p>

<p>Now, if I instead have</p>

<pre><code>class SomeClass():
    def__init__(self):
        self.property = method(param)
</code></pre>

<p>and I do:</p>

<pre><code>instance = SomeClass()
instance.property = 3
</code></pre>

<p>That code does not work, and I overwrite the reference to the object created by method(param) with 3, instead of having that setting handled by the descriptor.</p>

<p>Is there a way I can use descriptors without static methods? In essence, I need to be able to create several instances of the class, each with its own unique properties that can be altered using the convenient descriptor method. Is that possible?</p>

<p>Python version: 2.7</p>

<p>Thanks!</p>

<p>Properties are "computed attributes". Properties are implemented using descriptors. If an object's attribute is looked up and a descriptor object is found, the descriptor's getter function will compute the value. </p>

<p><strong>This special rule is valid only at the class level</strong>.</p>

<p>That's why:</p>

<pre><code>class SomeClass():
    property = &lt;a property object here&gt;
</code></pre>

<p>defines a property (computed attribute) for all <code>SomeClass</code> instances, but:</p>

<pre><code>class SomeClass():
    def__init__(self):
        self.property = &lt;a property object here&gt;
</code></pre>

<p>defines an ordinary attribute which happens to be of property object type, but is not treated specially in any way. Its getters and setters are ignored (unless called explicitely by the user code, of course).</p>

<hr>

<p>There is no other way of using descriptors and properties as setting them in the class and not in an instance.</p>

<p>UPDATE - Example:</p>

<p>Small example: instead of creating several instances each with its own descriptor (approach which does not work), a new subclass is instantiated every time.</p>

<pre><code>class BaseClass:
    pass

_class_number = 0 
def class_factory(prop):
    global _class_number
    _class_number += 1
    return type('Class' + str(_class_number), (BaseClass,), dict(prop=prop))

c1 = class_factory(property(fget=lambda self: "property1"))()
print(c1.prop)
c2 = class_factory(property(fget=lambda self: "property2"))()
print(c2.prop)
</code></pre>

python python-2.7