如何配置ipython以十六进制格式显示整数? [英] How can I configure ipython to display integers in hex format?

查看:150
本文介绍了如何配置ipython以十六进制格式显示整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是默认行为:

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

这就是我想要的:

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

我可以设置ipython吗?

Can I setup ipython to do that?

推荐答案

您可以通过为int注册特殊的显示格式化器来完成此操作:

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

如果你想要这个永远在线,你可以在 $(ipython locate profile)/startup/hexints.py 中用前两行创建一个文件(或者作为一个避免任何分配):

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))

将被执行每次启动IPython。

which will be executed every time you start IPython.

这篇关于如何配置ipython以十六进制格式显示整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆