TAGS :Viewed: 10 - Published at: a few seconds ago

[ How can I configure ipython to display integers in hex format? ]

Here's the default behavior:

In [21]: 255
Out[21]: 255

And here's what I would like:

In [21]: 255
Out[21]: FF

Can I setup ipython to do that?

Answer 1


You can do this by registering a special display formatter for ints:

In [1]: formatter = get_ipython().display_formatter.formatters['text/plain']

In [2]: formatter.for_type(int, lambda n, p, cycle: p.text("%X" % n))
Out[2]: <function IPython.lib.pretty._repr_pprint>

In [3]: 1
Out[3]: 1

In [4]: 100
Out[4]: 64

In [5]: 255
Out[5]: FF

If you want this always-on, you can create a file in $(ipython locate profile)/startup/hexints.py with the first two lines (or as one to avoid any assignments):

get_ipython().display_formatter.formatters['text/plain'].for_type(int, lambda n, p, cycle: p.text("%X" % n))

which will be executed every time you start IPython.