title: python: multiple inheritance and __add__() in base class
body: <p>I've got a base class where I want to handle <code>__add__()</code> and want to support when <code>__add__</code>ing two subclass instances - that is have the methods of both subclasses in the resulting instance.</p>
  
  <pre><code>import copy
  
  class Base(dict):
      def __init__(self, **data):
          self.update(data)
  
      def __add__(self, other):
          result = copy.deepcopy(self)
          result.update(other)
          # how do I now join the methods?
          return result
  
  class A(Base):
      def a(self):
          print "test a"
  
  class B(Base):
      def b(self):
          print "test b"
  
  
  if __name__ == '__main__':
      a = A(a=1, b=2)
      b = B(c=1)
      c = a + b
      c.b() # should work
      c.a() # should work
  </code></pre>
  
  <p><strong>Edit</strong>: To be more specific: I've got a class <code>Hosts</code> that holds a <code>dict(host01=.., host02=..)</code> (hence the subclassing of <code>dict</code>) - this offers some base methods such as <code>run_ssh_commmand_on_all_hosts()</code></p>
  
  <p>Now I've got a subclass <code>HostsLoadbalancer</code> that holds some special methods such as <code>drain()</code>, and I've got a class <code>HostsNagios</code> that holds some nagios-specific methods.</p>
  
  <p>What I'm doing then, is something like:</p>
  
  <pre><code>nagios_hosts = nagios.gethosts()
  lb_hosts = loadbalancer.gethosts()
  hosts = nagios_hosts + lb_hosts
  hosts.run_ssh_command_on_all_hosts('uname')
  hosts.drain() # method of HostsLoadbalancer - drains just the loadbalancer-hosts
  hosts.acknoledge_downtime() # method of NagiosHosts - does this just for the nagios hosts, is overlapping
  </code></pre>
  
  <p>What is the best solution for this problem?</p>
  
  <p>I think I can somehow "copy all methods" - like this:
      for x in dir(other):
          setattr(self, x, getattr(other, x))</p>
  
  <p>Am I on the right track? Or should I use Abstract Base Classes?</p>