I'm busy creating a metaclass that replaces the stub function on the class with a new one with the correct implementation. The original function can use any signature. My problem is that I cannot figure out how to create a new function with the same signature as the old one. How can I do it?
Update
This has nothing to do with the actual question, which is: "How do I dynamically create a function with the same signature as another function?" but I am adding this to show why I cannot use subclasses.
I am trying to implement something like Scala Case Classes in Python. (Does not match the pattern matching only automatically generated properties, eq, hashes, and str methods.)
I need something like this:
>>> class MyCaseClass(): ... __metaclass__ = CaseMetaClass ... def __init__(self, a, b): ... pass >>> instance = MyCaseClass(1, 'x') >>> instance.a 1 >>> instance.b 'x' >>> str(instance) MyCaseClass(1, 'x')
As far as I can see, there is no way with subclasses.
source share