Created
May 11, 2022 16:39
This file contains 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
class Hex(int): | |
def __repr__(self) -> str: | |
return hex(self) | |
_ops = ['__add__', '__sub__', '__mul__'] | |
def _monkey_patch_op(operation): | |
""" | |
wraps Hex() around int's magic | |
method for provided operation | |
""" | |
old_op = getattr(int, operation) | |
new_op = lambda self, val : Hex(old_op(self, val)) | |
setattr(Hex, operation, new_op) | |
for _op in _ops: | |
_monkey_patch_op(_op) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment