title: getattr and setattr on nested subobjects / chained properties?
body: <p>I have an object (<code>Person</code>) that has multiple subobjects (<code>Pet, Residence</code>) as properties. I want to be able to dynamically set the properties of these subobjects like so:</p>
  
  <pre><code>class Person(object):
      def __init__(self):
          self.pet = Pet()
          self.residence = Residence()
  
  class Pet(object):
      def __init__(self,name='Fido',species='Dog'):
          self.name = name
          self.species = species
  
  class Residence(object):
      def __init__(self,type='House',sqft=None):
          self.type = type
          self.sqft=sqft
  
  
  if __name__=='__main__':
      p=Person()
      setattr(p,'pet.name','Sparky')
      setattr(p,'residence.type','Apartment')
      print p.__dict__
  </code></pre>
  
  <p>Currently I get the wrong output: <code>{'pet': &lt;__main__.Pet object at 0x10c5ec050&gt;, 'residence': &lt;__main__.Residence object at 0x10c5ec0d0&gt;, 'pet.name': 'Sparky', 'residence.type': 'Apartment'}</code></p>
  
  <p>As you can see, instead of setting the <code>name</code> attribute on the <code>Pet</code> subobject of the <code>Person</code>, a new attribute <code>pet.name</code> is created on the <code>Person</code>. </p>
  
  <ul>
  <li><p>I cannot specify <code>person.pet</code> to <code>setattr()</code> because different sub-objects will be set by the same method, which parses some text and fills in the object attributes if/when a relevant key is found.</p></li>
  <li><p>Is there a easy/builtin way to accomplish this? </p></li>
  <li><p>Or perhaps I need to write a recursive function to parse the string and call <code>getattr()</code> multiple times until the necessary subobject is found and then call <code>setattr()</code> on that found subobject?</p></li>
  </ul>