I'm trying to use a function object as a method for a different class (via decorator), but instance binding isn't automatic. Of course, it makes sense that this shouldn't be automatic:
def decorator(method):
return Functor(method)
class Functor:
def __init__(self, decorated):
self.decorated = decorated
def __call__(self, instance, *args, **kwargs):
print(f"Calling on {instance = } with {args = }, {kwargs = }")
self.decorated(instance, *args, **kwargs)
class SomeClass:
@decorator
def some_method(self, a, b):
print(f"In some method with {a = }, {b = }")
instance = SomeClass()
## I want this to work...
# instance.some_method(1, 2)
## This works, but I want binding like the above one...
instance.some_method(instance, 1, 2)
How do I make the above snippet work with instance.some_method(1, 2)
?
I think the answer lies with descriptors, but it's not immediately clear what I need to do to get this to bind my instance / make Functor a "method" rather than a simple function.