If you want the hex form to be the actual representation of your object, you would need to sub-class int and implement __repr__:
class Hex(int):
def __new__(cls, arg, base=16):
if isinstance(arg, basestring):
return int.__new__(cls, arg, base)
return int.__new__(cls, arg)
def __repr__(self):
return '0x{:X}'.format(self)
Here I have also implemented __new__ to make 16 (rather than 10) the default base, so you can do things like:
>>> Hex('FFFF') # base defaults to 16
0xFFFF
>>> Hex('42', 10) # still supports other bases
0x2A
>>> Hex(65535) # and numbers
0xFFFF
You will also have to emulate a numeric type to allow Hex instances to be used in addition, subtraction, etc. (otherwise you'll just get a plain int back).