Skip to content

Instantly share code, notes, and snippets.

@stephanh42
Created June 3, 2017 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephanh42/a4d6d66b10cfecf935c9531150afb247 to your computer and use it in GitHub Desktop.
Save stephanh42/a4d6d66b10cfecf935c9531150afb247 to your computer and use it in GitHub Desktop.
"""
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