title: How to use default property descriptors and successfully assign from __init__()?
body: <p>What's the correct idiom for this please?</p>
  
  <p>I want to define an object containing properties which can (optionally) be initialized from a dict (the dict comes from JSON; it may be incomplete). Later on I may modify the properties via setters.
  There are actually 13+ properties, and <strong>I want to be able to use default getters and setters</strong>, but that doesn't seem to work for this case:</p>
  
  <p>But I don't want to have to write explicit descriptors for all of <code>prop1... propn</code>
  Also, I'd like to move the default assignments out of <code>__init__()</code> and into the accessors... but then I'd need expicit descriptors.</p>
  
  <p>What's the most elegant solution? (other than move all the setter calls out of <code>__init__()</code> and into a method/classmethod <code>_make()</code>?)</p>
  
  <p>[DELETED COMMENT The code for badprop using default descriptor was due to comment by a previous SO user, who gave the impression it gives you a default setter. But it doesn't - the setter is undefined and it necessarily throws <code>AttributeError</code>.]</p>
  
  <pre><code>class DubiousPropertyExample(object):
      def __init__(self,dct=None):
          self.prop1 = 'some default'
          self.prop2 = 'other default'
          #self.badprop = 'This throws AttributeError: can\'t set attribute'
          if dct is None: dct = dict() # or use defaultdict 
          for prop,val in dct.items():
              self.__setattr__(prop,val)
  
      # How do I do default property descriptors? this is wrong
      #@property
      #def badprop(self): pass
  
      # Explicit descriptors for all properties - yukk
      @property
      def prop1(self): return self._prop1
      @prop1.setter
      def prop1(self,value): self._prop1 = value
  
      @property
      def prop2(self): return self._prop2
      @prop2.setter
      def prop2(self,value): self._prop2 = value
  
  dub = DubiousPropertyExample({'prop2':'crashandburn'})
  
  print dub.__dict__
  # {'_prop2': 'crashandburn', '_prop1': 'some default'}
  </code></pre>
  
  <p>If you run this with line 5 <code>self.badprop = ...</code> uncommented, it fails:</p>
  
  <pre><code>self.badprop = 'This throws AttributeError: can\'t set attribute'
  </code></pre>
  
  <p>AttributeError: can't set attribute</p>
  
  <p>[As ever, I read the SO posts on descriptors, implicit descriptors, calling them from <strong>init</strong>]</p>