python - 如何配置 ipython 以十六进制格式显示整数?

标签 python ipython

这是默认行为:

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

这就是我想要的:

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

我可以设置 ipython 来做到这一点吗?

最佳答案

您可以通过为整数注册一个特殊的显示格式化程序来做到这一点:

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 中用前两行创建一个文件(或者作为一个文件来避免任何分配) :

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

每次启动 IPython 时都会执行。

关于python - 如何配置 ipython 以十六进制格式显示整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14977066/

相关文章:

python - 如何使用 pyenv 安装 ipython qtconsole(Python 版本 3.4.2)

python - 正则表达式 - 查找 @ 之后和特定字符之前的文本

python - 如何在Python中读取字符串的一部分?

json - 无法读取的笔记本 : Unsupported JSON nbformat version 4 (supported version: 3)

python - 名称 'get_config' 未定义

matplotlib - cygwin : TclError: no display name and no $DISPLAY environment variable 上的 ipython --pylab

python - ipywidgets:根据另一个小部件的结果更新一个小部件

python - pyglet最近有变化吗?

python - 在 O(m*log m) 中计算 'initial lists' 的算法

python - python 列表切片的整数和引用