-
- # method wrapper 1
- class Foo:
- _methods = {...}
- def __getattr__(self, name):
- if name in _methods:
- return getattr(self._wrapped, name)
- raise AttributeError(name)
-
- # method wrapper 2
- class Foo:
- pass
-
- def add_wrapper(cls, method):
- def wrapper(self, *args, **kwargs):
- return getattr(self._wrapped, method)(*args, **kwargs)
- setattr(cls, method, wrapper)
-
- for method in methods:
- add_wrapper(Foo, method)
-
- # method wrapper 3
- class Foo:
- pass
-
- def add_wrapper(cls, method):
- code = text.dedent("""
- def wrapper(self, *args, **kwargs):
- return self._wrapped.{}(*args, **kwargs)
- """)
- ns = {}
- exec(code, ns)
- setattr(cls, method, ns["wrapper"])
-
- for method in methods:
- add_wrapper(Foo, methods)
-