Skip to content

Instantly share code, notes, and snippets.

@strager
Created December 29, 2020 22:11
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 strager/7e0692e8cbe1f24d3046fafc22ab216e to your computer and use it in GitHub Desktop.
Save strager/7e0692e8cbe1f24d3046fafc22ab216e to your computer and use it in GitHub Desktop.
from functools import singledispatchmethod
class Negator:
@singledispatchmethod
def neg(self, arg):
raise NotImplementedError("Cannot negate a")
@neg.register
def _(self, arg: int):
return -arg
@neg.register
def _(self, arg: bool):
return not arg
print(Negator().neg(100))
print(Negator().neg(False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment