python 解释器中的 more-ing 或 less-ing 输出

more-ing or less-ing output in the python interpreter

在解释器模式下 运行 python 时 more-ing 或 less-ing 多行输出的最佳替代方法是什么?

假设,存在一个对象变量foo,它有很多属性。 dir(foo) 会转储到屏幕上。我们无法检查或分页此输出,因为您会立即看到解释器提示。

目前检查此类数据的唯一方法是存储到变量中并查看切片或它。例如

>>> keys = dir(foo)
>>> len(keys)
120
>>> keys[10:20] #viewing the sub slice of keys
...

希望有替代方案。我知道 help() 确实提供了一个更类似的界面,但仅用于正在考虑的对象的文档。

help 的更像界面由 pydoc module, in particular its undocumented method pager. If you convert your data to a string (perhaps by using the pprint 模块提供,以提高可读性),您可以将其发送到 pager 以获得您正在寻找的交互式可视化对于.

>>> import pydoc
>>> import pprint
>>> def more_vars(obj):
...     pydoc.pager(pprint.pformat(vars(obj)))
...
>>> import math
>>> more_vars(math)
{'__doc__': 'This module provides access to the mathematical functions\n'
            'defined by the C standard.',
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__name__': 'math',
 '__package__': '',
 [not pictured: about 30 more lines of methods/attributes]
 'frexp': <built-in function frexp>,
 'fsum': <built-in function fsum>,
 'gamma': <built-in function gamma>,
-- More  --