I wish to display some variables, data members in hexadecimal format any time they are printed.
My current approach is to define a class Hex:
class Hex:
"""Facilitate printing an integer in hexadecimal format."""
def __init__(self, data):
if not isinstance(data, int):
raise ValueError("Hex expects an integer.")
self._data = data
def __repr__(self):
return hex(self._data)
def __eq__(self, other):
return self._data == other
def __lt__(self, other):
return self._data < other
def __le__(self, other):
return self._data <= other
def __gt__(self, other):
return self._data > other
def __ge__(self, other):
return self._data >= other
def __add__(self, right):
return self._data + right
def __radd__(self, left):
return self._data + left
def __hash__(self):
return hash(self._data)
This works all right as the following examples demonstrate it:
address = Hex(0xdeadbeef)
record1 = {'address': address, 'counter': 42}
print(record1)
{'address': 0xdeadbeef, 'counter': 42}
The __hash__ is defined so that the object is hashable and can be used as a key in a dict:
record2 = {address: 'address'}
print(record2)
{0xdeadbeef: 'address'}
The equality and comparisons are defined so that handling of duplicates and sorting works:
list1 = [Hex(0xff), 15, 23, Hex(0xf), 255]
print(list1)
[0xff, 15, 23, 0xf, 255]
print(set(list1))
{15, 23, 0xff}
list1.sort()
print(list1)
[15, 0xf, 23, 0xff, 255]
The __add__ and __radd__ are defined so that pointer arithmetic is supported:
new_address = Hex(record1['address'] + 0x20400000)
print(new_address)
0xfeedbeef
address += 0x3f00
address += Hex(0xfe)
print(address)
0xdeadfeed
And now to the questions.
Is there a built in or existing hex integer that somehow has its own __repr__ attached that prints it in hex, but otherwise it would work as an int. I could not find such hence the above class.
The above pointer arithmetic works (subtraction, negation can be added similarly) and I was surprised that += works as well. Should I still add __iadd__?
Should I add/override __new__? Something like the following would not create duplicate instances for the same value:
def __new__(cls, *args, **kwargs):
if not hasattr(cls, 'instances'):
cls.instances = {}
data = args[1]
if data in cls.instances:
return cls.instances[data]
# Create if not found:
inst = super(Hex, cls).__new__(cls) #, *args, **kwargs)
cls.instances[data] = inst
return inst
Any other suggestion to fix the class Hex or make it better?
+=works.address += Hex(0xfe)doesn't get the specified results.class Hex(int): ...then simply implement__repr__the way you did abovea = Hex(0xf); a += Hex(0x1); aprints16. When I tried to overwrite__add__I got an infinite loop until I figured that__add__should return:Hex(int(self) + other)To @Bharel: There are two additions, 0xef00 and 0xfe.