'Make one class without inheritance from class that inherits from other classes

I have some inheritance structure of classes. For example, I have class A in a file a_class.py (it has init method and two additional methods: b and c)

class A:
    D = 345
    def __init__(self, a):
        self.a = a
        print(a)
       
    def b(self):
        print(f'b A {self.a}')
    
    def c(self):
        print(f'c A {self.a}')

Then I have class B from b_class.py (it inherits from A, overrides b and init, calls with super init of parent class, and introduces its own method a)

from a_class import A

class B(A):
    A = 123
    def __init__(self, a):
        super().__init__(a+1)
        self.b = a
        print(a)
       
    def b(self):
        print(f'b B {self.b} {self.a}')
    
    def a(self):
        print(f'c B {self.b} {self.a}')
    

Then I want to have some function that can take B class and remove all dependencies on parents classes up to some parent class. So, in this case, it should be something like this:

class C:
    A = 123
    D = 345

    def __init__(self, a):
        a_A = a + 1
        self.a = a_A
        print(a_A)
        self.b = a
        print(a)
       
    def b(self):
        print(f'b B {self.b} {self.a}')
    
    def a(self):
        print(f'c B {self.b} {self.a}')
    
    def c(self):
        print(f'c A {self.a}')

So, this function must work on any inheritance configuration possible in python and also it would be really good if I could specify which classes can be inherited. For example, if I have the following class structure: object -> A -> B -> C -> D. Then I can say make new class E from class D, and specify that it must remove all inheritance up to B, so I will have the following structure: object -> A -> B -> E.

I tried looking for such function or even something similar, but haven't found anything. I think it is possible to do, but may be not easy. I think I can use inspect.getmembers method to get all elements of class.

all_functions = inspect.getmembers(B, inspect.isfunction)
for func in all_functions:
    name_func, func_method = func
    print(inspect.getsource(func_method)) 

This will print:

def __init__(self, a):
    super().__init__(a+1)
    self.b = a
    print(a)

def a(self):
    print(f'c B {self.b} {self.a}')

def b(self):
    print(f'b B {self.b} {self.a}')

def c(self):
    print(f'c A {self.a}')

But as I see it now, the main problem is with calling overriden methods. So, I need to find somehow what functions are used inside some function. Then I need to see that this is inherited function. Then I need to get this function and "unfold" it. To "unfold" the code analyzes must be used (ast or dis modules maybe). Can you please point where should I look next? Or is there some ready github repo for such type of tasks. I can't even google this question. Nothing is found.

Edit: Some motivation

So suppose I have class Animal

class Animal:
    def eat(self):
        print('eat')
    
    def sleep(self):
        print('sleep')

Then I decide to create class Dog. Pretty easy. But there are some variants:

class Dog1(Animal):
    def bark(self):
        print('woof woof')
        
class Dog2(Animal):
    def bark(self):
        print('yaf yaf')

class Dog1_jumpy(Dog1):
    def jump(self):
        print('jump')

class Dog2_cat(Dog2):
    def look(self):
        print('look for cats')

After testing, looks like Dog2_cat is the best one for me. So, I want to finalize Dog class and remove all unnecessary experiments (Dog1, Dog2, Dog1_jumpy).

Now I have to do something like this. And then rename Dog2_cat_long to Dog:

class Dog1(Animal):
    def bark(self):
        print('woof woof')
        
class Dog2(Animal):
    def bark(self):
        print('yaf yaf')

class Dog1_jumpy_long(Animal):
    def bark(self):
        print('woof woof')
    
    def jump(self):
        print('jump')

class Dog2_cat_long(Animal):
    def bark(self):
        print('yaf yaf')
        
    def look(self):
        print('look for cats')

I don't want to carry all functions in every class. Also, I don't want to manually create class Dog from Dog2_cat. I want to call my func uninherit(in_class=Dog2_cat, base_class=Animal, return_name=Dog) and get my best class.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source