Yes, you can merge two class instances in this simple manner.
Merging Class Instances
# merging two class instances
# works with Python26 and Python31
class A:
pass
class B:
pass
inst_a = A()
inst_b = B()
inst_a.alpha = 333
inst_b.beta = 777
def merge(ob1, ob2):
"""
an object's __dict__ contains all its
attributes, methods, docstrings, etc.
"""
ob1.__dict__.update(ob2.__dict__)
return ob1
# merge the two class instances into one instance
inst_a = merge(inst_a, inst_b)
# you can delete instance inst_b (optional)
del inst_b
sf = "instance inst_a now has alpha=%s and beta=%s attributes"
print(sf % (inst_a.alpha, inst_a.beta))
'''
instance inst_a now has alpha=333 and beta=777 attributes
'''
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.