Created
June 3, 2017 06:30
-
-
Save stephanh42/a4d6d66b10cfecf935c9531150afb247 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Support `fake` binary operator | |
so we can do | |
a @f@ b | |
in addition to (and with the same meaning as): | |
f(a, b) | |
""" | |
import collections.abc | |
class PartialBinop(object): | |
def __init__(self, callable, left_arg): | |
self.callable = callable | |
self.left_arg = left_arg | |
def __matmul__(self, right_arg): | |
return self.callable(self.left_arg, right_arg) | |
class BinopCallable(collections.abc.Callable): | |
def __init__(self, callable): | |
self.callable = callable | |
def __call__(self, *args, **kws): | |
return self.callable(*args, **kws) | |
def __rmatmul__(self, left_arg): | |
return PartialBinop(self.callable, left_arg) | |
#### Example | |
@BinopCallable | |
def add(x, y): | |
return x + y | |
print(3 @add@ 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment