The best answer in the comments, it was useful to me, so I decided to show it in response (thanks to sr2222): The path to dynamically declaring inheritance in Python is the built-in type () function. For my example:
class A(object) : def __init__(self, args): self.a = 'a' self.args = args def getattA(self): return self.a, self.args class B(object) : b = 'b' def __init__(self, args) : self.b_init = args def getattB(self): return self.b C = type('C', (A,B), dict(c='c')) instc = C('args') print 'attributes :', instc.a, instc.args, instc.b, instc.c print 'methodes :', instc.getattA(), instc.getattB() print instc.b_init
Return code:
attributes : a args bc methodes : ('a', 'args') b Traceback (most recent call last): File "D:\Documents and settings\Bureau\merge2.py", line 24, in <module> print instc.b_init AttributeError: 'C' object has no attribute 'b_init'
My class C initializes the attributes and methods of class A and class B, and we add the attribute c. With instanciation C (instc = C ('args')) init for A is a call, but not for B.
Very useful for me, because I need to add some attributes and methods (the same) in different classes.
source share