Where does a python descriptor's state go?
<h3>Background</h3>

<p>I'm trying to figure out <a href="http://docs.python.org/reference/datamodel.html#implementing-descriptors">Python's descriptors</a> by reading Lutz's <a href="http://rads.stackoverflow.com/amzn/click/0596158068">Learning Python's</a> <a href="http://my.safaribooksonline.com/book/programming/python/9780596805395/37dot-managed-attributes/957">section on the topic</a> in which he says: <em>"Like properties, descriptors are designed to handle specific attributes... Unlike properties, descriptors have their own state..."</em></p>

<p>Throughout the chapter he shows examples in which the managed attribute is actually stashed on the containing/wrapping object, as in: </p>

<pre><code>def __set__(self, instance, value):
    instance._name = value.lower()
</code></pre>

<p>I understand these examples and they seem to be common in <a href="http://martyalchin.com/2007/nov/24/python-descriptors-part-2-of-2/">write ups</a> on the topic. That said, their benefit over <a href="http://docs.python.org/library/functions.html#property">properties</a> isn't obvious to me and they seem to fall short of the internal state promised in the above quote. </p>

<p>At the end of the chapter he shows an example that is closer to what I pictured after reading "have their own state", as in:</p>

<pre><code>def __set__(self, instance, value):
    self.name = value.lower()
</code></pre>

<p>The example runs but does not do what I'd expect it to do. As <a href="http://pastebin.com/VdCfV5BJ">the example</a> is a bit long I've put it on Pastebin and added a last line that shows the unexpected behavior (Bob's name is now Sue). Here's a shorter demo snippet: </p>

<pre><code>class Wrapper(object):
    class ExampleDescriptor(object):
        def __get__(self, instance, owner):
            print "get %s" % self.state
            return self.state

        def __set__(self, instance, value):
            print "set %s" % value
            self.state = value
    ex = ExampleDescriptor()


w1 = Wrapper()
w1.ex = 1
print w1.ex
w2 = Wrapper()
print w2.ex
w2.ex = 2
print  w1.ex
print w1.ex is w2.ex
</code></pre>

<p>The output of which is: </p>

<pre><code>set 1 
get 1
1
get 1
1
set 2
get 2
2
get 2
get 2
True
</code></pre>

<p>None of this execution comes as a surprise after looking at the code carefully. The validation logic in the descriptor is making a de facto singleton out of this attribute on the wrapper class; however, it's hard to imagine this shared state was Lutz's intention, or the <a href="http://pastebin.com/ckRKxch7">intention</a> in this <a href="http://users.rcn.com/python/download/Descriptor.htm#descriptor-example">widely linked tutorial</a> on the topic. </p>

<h3>Question</h3>

<p>Is it possible to make a descriptor that has internal state that is unique to the wrapping object without stashing that state on the wrapping object instances (as in the first snippet)? Is it possible to modify the <code>CardHolder</code> class from the <a href="http://pastebin.com/VdCfV5BJ">linked example</a> such that Bob does not end up as Sue?</p>
