'What is the best to extend functionality of a Library class method

I have a library code that provides class and that class is expected to be a subclass of my frameworks classes.

Now I want to extend one of the functionalities of a library class method, here I can think of 2 approaches:

  1. create a new class with subclass library class then override library class method in a new class
  2. create Mixins override the functionality of the library class method and inherit that as well Here is a coded way of my above 2 theoretical approaches.

approach 1:

class Library:

    def method_one(self):
        return {"method_one": "one"}

    def method_two(self):
        return {"method_two": "two"}


class FrameworkOne(Library):
    def method_one(self):
        method = super().method_one()
        method["label"] = self.__class__.__name__
        return method
        
        
class FrameworkTwo(Library):
    def method_one(self):
        method = super().method_one()
        method["label"] = self.__class__.__name__
        return method

approach 2:

class MixinFramework:
    def method_one(self):
        method = super().method_one()
        method["label"] = self.__class__.__name__
        return method


class FrameworkOne(MixinFramework, Library):
    pass


class FrameworkTwo(MixinFramework, Library):
    pass

another way I can think of is to create another Baseclass which will inherit Library class and override method_one in Base class and inherit Baseclass in all Framework classes instead of Library class

approach one seems to code redundancy issue and approach two seems to me like narrow down is there any better approach or a decorator way that I can put on to each class where I want to extend the functionality of method_one Please suggest.



Sources

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

Source: Stack Overflow

Solution Source