Suppose I have a Function class whose instances are callers that take one argument. I defined point arithmetic for these classes in the literal sense. Here is a simplified version of my code (I actually have more complex behavior in __init__ and __call__ , but that doesn't matter for this question):
class Function: ''' >>> f = Function(lambda x : x**2) >>> g = Function(lambda x : x + 4) >>> h = f/g >>> h(6) 3.6 ''' def __init__(self, func): self.func = func def __call__(self, value): return self.func(value) def __truediv__(self, other): if isinstance(other, Function): return Function(lambda x:self(x)/other(x)) else: return NotImplemented
I get stuck when I try to resolve implicit type conversions. For example, I want to write:
>>> f = Function(lambda x : x ** 2) >>> g = f+1 >>> g(5) 26
In other words, whenever I see a numeric object v in an arithmetic expression next to an instance of Function , I want to convert v to Function(lambda x : v) .
In addition, I want to achieve similar behavior for some of my custom types (again, when I see them in the same binary arithmetic expression with a Function object).
Although I can, of course, encode this logic using a brute-force set of regular and reflected binary arithmetic operators, each of which checks isinstance(v, numbers.Number) and isinstance(v, MyUserDefinedType) , I believe there may be a more elegant way .
Also, if other improvements are possible in my design, let me know. ( Function objects are rarely created, but are called very often, so performance is of some interest.)
EDIT:
To address the @Eric comment, I have to clarify that I have another custom Functional class:
class Functional: ''' >>> c = [1, 2, 3] >>> f = Functional(lambda x : x + 1) >>> f(c) [2, 3, 4] >>> g = Functional(lambda x : x ** 2) >>> h = f + g >>> h(c) [3, 7, 13] ''' def __init__(self, func): self.func = func @staticmethod def from_function(self, function): return Functional(function.func) def __call__(self, container): return type(container)(self.func(c) for c in container) def __add__(self, other): if isinstance(other, Functional): return Functional(lambda x : self.func(x) + other.func(x)) else: return NotImplemented
When I see an instance of Function and Functional in the same arithmetic expression, I want Function be implicitly converted to Functional using the Functional.from_function method.
So, the implicit type conversion hierarchy is as follows:
- Functional
- Function
- anything else
And I would like to implicitly convert to the highest type in this hierarchy, visible in this arithmetic expression.