python解释器中的多或少输出

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

假设存在一个具有许多属性的对象变量foodir(foo)将转储到屏幕上。由于您会立即看到翻译提示,因此我们无法检查或分页此输出。

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

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

希望有替代方法。我知道help()确实提供了更相似的界面,但仅用于考虑中对象的文档。

zht_410728 回答:python解释器中的多或少输出

help的界面更像由pydoc模块提供,尤其是其未公开的方法pager。如果您将数据转换为字符串(也许通过使用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  --
本文链接:https://www.f2er.com/3165223.html

大家都在问