medium +20 pts

Class Method Factory

Build a factory that generates methods for a class based on a mapping.

Implement a function `create_class_with_factory(mapping)` that creates a new class (subclass of `MethodFactory`) and uses the class method `add_methods` of `MethodFactory` to add methods to it. You must also implement the `MethodFactory` class with a class method `add_methods(cls, mapping)` that adds new methods to the class. The `mapping` is a dictionary where each key is a string (method name) and each value is a callable that takes the same number of arguments as the desired method, excluding `self`. The added methods should be instance methods: when called on an instance, they receive the instance as the first argument (self) and then the arguments from the mapping callable. The added methods should simply delegate to the callable, passing `self` and any additional arguments. They should be stored in the class's namespace so that `hasattr(cls, method_name)` returns `True` after adding. If a method name in `mapping` already exists in the class, it should be replaced (overwritten). The `add_methods` method should return the class itself (to allow chaining). For example, if you add a method `greet` that takes `self` and a name, then `instance.greet('Alice')` should call the underlying callable with `(instance, 'Alice')`. `create_class_with_factory(mapping)` should return a **string** `"works"` after successfully adding the methods to the generated class. (This is a helper to verify the factory works; the test will call it and check the return value.) **Function Signatures:** ```python class MethodFactory: @classmethod def add_methods(cls, mapping): ... def create_class_with_factory(mapping): ... ``` **Note:** The mapping callable is a plain function that expects `self` as its first parameter, not a descriptor or pre-bound method.

Constraints

- `mapping` is a dict with string keys (valid Python identifiers) and callable values. - The callable should accept at least one argument (the instance). - At most a few hundred methods can be added. - Must not use external libraries.

Example

>>> class MyClass(MethodFactory):
...     pass
>>> def greet(self, name):
...     return f"Hello, {name}!"
>>> MyClass.add_methods({'greet': greet})
<class '__main__.MyClass'>
>>> obj = MyClass()
>>> obj.greet('Alice')
'Hello, Alice!'
>>> def add(self, a, b):
...     return a + b
>>> class Calc(MethodFactory):
...     pass
>>> Calc.add_methods({'add': add, 'multiply': lambda self, a, b: a * b})
<class '__main__.Calc'>
>>> c = Calc()
>>> c.add(2, 3)
5
>>> c.multiply(2, 3)
6
>>> create_class_with_factory({'greet': lambda self, name: 'Hi ' + name})
'works'
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use setattr to dynamically add methods to the class.
You can define a wrapper function inside add_methods that takes *args, **kwargs and calls the mapping function with self prepended.
Remember to return cls from add_methods to allow chaining.
In create_class_with_factory, create a subclass of MethodFactory, call add_methods on it, then return the string 'works'.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.