I am working on a Python project where I need to dynamically create methods for a class at runtime. I want to use a decorator to add these methods to the class, based on some external configuration. The requirements are:
The decorator should read method definitions from an external configuration (e.g., a dictionary).
The decorator should dynamically add these methods to the class.
Each generated method should have its own unique implementation as specified in the configuration.
Here is a rough outline of what I am imaging:
method_config = {
'method1': lambda self: "This is method1",
'method2': lambda self, arg: f"Method2 received {arg}"
}
@dynamic_methods(method_config)
class MyClass:
...