Python 2.7
It is possible to inherit properties from class to class:
class A: def __init__(self, a): self.aa = a class B(A): def __init__(self, a, b): A.__init__(self, a) self.bb = b c = B(10, "qwerty") print c.aa, c.bb In this example, a child can only be created from scratch - anew. And how to inherit the initialized parent? Those. take and from a specimen c spawn descendants?
"Frontal" option: the input parameter is the class and override everything from it, but I would like to avoid this, because have to sort through all the properties and methods in a new way.
What interests me is the transfer of all properties and methods from an object to another through initialization or a method. Also, the problem can be solved by merging two classes for example.
For example, we have 2 classes (imagine that there is not one but a huge number of properties and methods):
class A: def __init__(self, a): self.aa = a def get_list_aa(self): return list(self.aa) class B: def __init__(self, b): self.bb = b def get_tuple_bb(self): return tuple(self.bb) I try to get C1 - just merge two initialized objects-classes, or C2 - use in the basis (inheritance from the initialized object-class in fact) - the result of this content:
class C1: def __init__(self): self.aa = 'any value' self.bb = 'any_value' def get_list_aa(self): return list(self.aa) def get_tuple_bb(self): return tuple(self.bb) class C2: def __init__(self, b): self.aa = 'any value' self.bb = b def get_list_aa(self): return list(self.aa) def get_tuple_bb(self): return tuple(self.bb)
c, then simply add a method to classBthat allows you to get a copy of the instance. - Xanderclonemethod, which will return a copy of the current one - gil9red__dict__- Eugene Dennis