Skip to content

Instantly share code, notes, and snippets.

@JakeTheCorn
Created July 26, 2020 02:24
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 JakeTheCorn/415141fdb69e3eb114934518a29f48d1 to your computer and use it in GitHub Desktop.
Save JakeTheCorn/415141fdb69e3eb114934518a29f48d1 to your computer and use it in GitHub Desktop.
An idea I had to code comparisons... The idea is that you can't "have" a comparison without two things to compare, a, b... so it could go in a constructor. This could also be useful as a template method pattern.
class Comparison:
def __init__(self, a, b, **kwargs):
self.a = a
self.b = b
self.kwargs = kwargs
def compare(self):
""" .compare() -> Union[1, -1, 0]"""
if self._a_has_higher_precedence():
return 1
if self._a_has_equal_precedence():
return 0
if self._a_has_lower_precedence():
return -1
return 0. # or raise?
def _a_has_higher_precedence(self):
pass
def _a_has_lower_precedence(self):
pass
def _a_has_equal_precedence(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment