Skip to content

Instantly share code, notes, and snippets.

@nicktimko
Last active May 14, 2018 14:20
Show Gist options
  • Save nicktimko/a85ac7c7d634421aeec02a8bee48bdbd to your computer and use it in GitHub Desktop.
Save nicktimko/a85ac7c7d634421aeec02a8bee48bdbd to your computer and use it in GitHub Desktop.
python arbitrary binary operators
import functools
class Operator:
def __init__(self, func):
self.func = func
def __rlshift__(self, other):
return self.__class__(functools.partial(self.func, other))
def __rshift__(self, other):
return self.func(other)
@Operator
def xor(a, b):
return bool(a) ^ bool(b)
assert False <<xor>> False == False
assert False <<xor>> True == True
assert True <<xor>> False == True
assert True <<xor>> True == False
@Operator
def tripledouble(a, b):
return a*3 + b*2
'a' <<tripledouble>> 'b' # aaabb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment