subclassing int to attain a Hex representation
问题 Basically I want to have access to all standard python int operators, eg __and__ and __xor__ etc, specifically whenever the result is finally printed I want it represented in Hex format. (Kind of like putting my calculator into Hex mode) class Hex(int): def __repr__(self): return "0x%x"%self __str__=__repr__ # this certainly helps with printing if __name__=="__main__": print Hex(0x1abe11ed) ^ Hex(440720179) print Hex(Hex(0x1abe11ed) ^ Hex(440720179)) Ideally BOTH line of output should be