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) 
  • No Inheritance is possible only from the class. What problem are you solving? If you need copies of object c , then simply add a method to class B that allows you to get a copy of the instance. - Xander
  • I want to create a new object1 with a sampling of the values ​​of properties and methods from an already initialized object2 - Eugene Dennis
  • @EugeneDennis, write the clone method, which will return a copy of the current one - gil9red
  • @ gil9red I added a response with the option I stopped at, during it is initialized, clone is done through parsing __dict__ - Eugene Dennis

2 answers 2

The term "inheritance" applies only to classes. What you want to do is more correctly called "copying" objects.

You can implement, for example, like this:

 class A: def __init__(self, a): self.aa = a class B(A): def __init__(self, a, b): A.__init__(self, a) self.bb = b # Добавляем этот метод def copy(self): return self.__class__(self.aa, self.bb) c_1 = B(10, "qwerty") print c_1.aa, c_1.bb c_2 = c_1.copy() print c_2.aa, c_2.bb 

    Option, through busting __dict__ , "tore", but it works:

     import functools class A: def __init__(self, a): self.aa = a def get_list_aa(self): return list(self.aa) class B: def __init__(self, parent, b): self.bb = b for attr, val in parent.__dict__.iteritems(): if attr.startswith("__"): continue self.__dict__[attr] = val for attr, val in parent.__class__.__dict__.iteritems(): if attr.startswith("__"): continue if not callable(val): continue self.__dict__[attr] = functools.partial(val, self) def get_tuple_bb(self): return tuple(self.bb) insta = A('first value') instb = B(insta, 'second value') print instb.__dict__ print instb.get_list_aa() print instb.get_tuple_bb()