0

I have a composite class A that contains some component classes like B. To elegantly call the methods contained within component classes, I added some wrapper functions like foo where the component method is called in a single line. However, I want to know if there is any way to dynamically add the methods of class B to class A using a decorator without having to manually include these methods inside the composite.

class A: # composite
    def __init__(self):
        self.B = B()
        
    def foo(self):
        # Instead of calling self.B.foo(...)
        # I want to simply call self.foo(...)
        # Then this method will not be needed
        self.B.foo()

class B: # component
    def foo(self):
        print("foo")
cweck
  • 1
  • What you're trying to create is a proxy class. Google it. Related: https://stackoverflow.com/questions/9942536/how-to-fake-proxy-a-class-in-python – Barmar Aug 10 '21 at 06:59
  • have a look at libraries like delegable (https://github.com/suzukimilanpaak/delegable) – marmeladze Aug 10 '21 at 06:59
  • Please refer https://stackoverflow.com/questions/3782040/python-decorators-that-are-part-of-a-base-class-cannot-be-used-to-decorate-membe – Rinshan Kolayil Aug 10 '21 at 07:21

0 Answers0