title: How to call methods on Python class descriptor objects?
body: <p>I created a <code>class String()</code> with <code>__get__()</code>, <code>__set__()</code>, and a method <code>to_db()</code>; however, when I do <code>name = String()</code>, I can't do <code>self.name.to_db()</code> because it's calling <code>to_db()</code> on the value returned by <code>__get__()</code>, not the object "<code>name</code>". </p>
  
  <pre><code>class String(object):
  
      def __init__(self, value=None):
          if value:
              self.value = str(value)
  
      def __get__(self, instance, owner):
          return self.value
  
      def __set__(self, instance, value):
          self.value = str(value)
  
      def to_db(self):
          return {'type':'string', 'value': self.value}
  
  class Example(object):
  
      name = String()
      age = Integer()
  
      def __init__(self,name,age):
          self.name = name
          self.age = age
  
      def save():
          data = dict(name=self.name.to_db(), age=self.age.to_db())
          db.save(data)
  </code></pre>
  
  <p>One way to deal with this is to not call <code>self.name.to_db()</code> directly and instead set a flag in <code>instance</code> and create a conditional in <code>__get__()</code> to check for it and call <code>to_db()</code> if it's <code>True</code>, but this seems kludgy. Is there a better way?</p>
  
  <p>Also, I'm new to descriptors -- what are the pros/cons of using <code>instance</code> and/or <code>instance.__dict__</code> to store state vs storing it in <code>self</code>?</p>