I have a base class where I want to process __add__()and want to support when __add__with two instances of a subclass that have methods of both subclasses in the resulting instance.
import copy
class Base(dict):
def __init__(self, **data):
self.update(data)
def __add__(self, other):
result = copy.deepcopy(self)
result.update(other)
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()
c.a()
Change . To be more specific: I have a class Hoststhat contains dict(host01=.., host02=..)(hence a subclassification dict) - this offers some basic methods, such asrun_ssh_commmand_on_all_hosts()
Now I have a subclass HostsLoadbalancerthat contains some special methods, such as drain(), and I have a class HostsNagiosthat contains some nagios-specific methods.
What I do then is something like:
nagios_hosts = nagios.gethosts()
lb_hosts = loadbalancer.gethosts()
hosts = nagios_hosts + lb_hosts
hosts.run_ssh_command_on_all_hosts('uname')
hosts.drain()
hosts.acknoledge_downtime()
What is the best solution for this problem?
, - " " - : x in dir (): setattr (self, x, getattr (other, x))
? ?