Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created May 11, 2022 16:39
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