Python console default hex display

The regular Python interpreter will call sys.displayhook to do the actual displaying of expressions you enter. You can replace it with something that displays exactly what you want, but you have to keep in mind that it is called for all expressions the interactive interpreter wants to display:

>>> import sys
>>> 1
1
>>> "1"
'1'
>>> def display_as_hex(item):
...     if isinstance(item, (int, long)):
...         print hex(item)
...     else:
...         print repr(item)
...
>>> sys.displayhook = display_as_hex
>>> 1
0x1
>>> "1"
'1'

I suspect you’ll quickly get tired of seeing all integers as hex, though, and switch to explicitly converting the ones you want to see as hex accordingly.

Leave a Comment