Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created May 11, 2022 16:39
Show Gist options
  • Save mentix02/243c7b1f2102f1375c577c943a2a2045 to your computer and use it in GitHub Desktop.
Save mentix02/243c7b1f2102f1375c577c943a2a2045 to your computer and use it in GitHub Desktop.
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