如何用更短和自定义的内容选择性地覆盖 python 的帮助(MYclass)中的文本?
How to selectively override the text from python's help(MYclass) with something shorter and customized?
我正在学习编写用户友好的 类 和方法,我希望用户知道如何从终端使用它们。 Python的标准help(MYclass)
returns十八行我不要,小window就半屏,刚学的python会输与前面几行的连续性从顶部消失。
有什么方法可以覆盖使用 help(MYclass)
(或 help(MYmethod)
)显示的内容,以便它只显示(在本例中)单行文档字符串?
虽然一些 IDE 在气泡中显示文档字符串,但终端不合作。 (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):
因此,我转而向 help
寻求帮助,但是 help
对于所有这些额外的模板行 过于有用。
然后我想到重新定义帮助:
def hhelp(x):
return x.__doc__
help = hhelp
但我认为这是邪恶的;比如重新定义数字 7,我希望 help(some builtin)
仍然正常工作,并且选择性劫持只发生在 MYclass
es.
总有
def doc(x):
return x.__doc__
如果我找不到任何选择性劫持的东西 help()
。
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
结果为十九行。我只想显示我的一行文档字符串。
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
因为 help(help)
表示它依赖于 pydoc
,特别是 pydoc.help
.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
检查源代码,我们发现它依赖于 TextDoc.docclass
来生成 类 的帮助文本。所以我们可以 monkeypatch 该方法以生成我们自己的帮助文本:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
现在我们只会在帮助文本中得到 __doc__
字符串:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1.该语法特定于 Python 2.7,因为 OP 使用相应的标签。虽然它似乎也适用于其他版本的 Python。
您可以对内置的 help
函数进行 monkeypatch 以显示 类 的 __doc__
字符串并返回到原始的 help
以显示其他对象:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
产生以下输出:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.
我正在学习编写用户友好的 类 和方法,我希望用户知道如何从终端使用它们。 Python的标准help(MYclass)
returns十八行我不要,小window就半屏,刚学的python会输与前面几行的连续性从顶部消失。
有什么方法可以覆盖使用 help(MYclass)
(或 help(MYmethod)
)显示的内容,以便它只显示(在本例中)单行文档字符串?
虽然一些 IDE 在气泡中显示文档字符串,但终端不合作。 (Do the triple-quoted (docstring) messages in Python appear while typing in IDEs other than IDLE?):
因此,我转而向 help
寻求帮助,但是 help
对于所有这些额外的模板行 过于有用。
然后我想到重新定义帮助:
def hhelp(x):
return x.__doc__
help = hhelp
但我认为这是邪恶的;比如重新定义数字 7,我希望 help(some builtin)
仍然正常工作,并且选择性劫持只发生在 MYclass
es.
总有
def doc(x):
return x.__doc__
如果我找不到任何选择性劫持的东西 help()
。
class A(object):
"""instantiate a = A(x), then use a.sqr() to get x**2"""
def __init__(self, x):
self.x = x
def sqr(self):
return x**2
结果为十九行。我只想显示我的一行文档字符串。
Help on class A in module __main__:
class A(__builtin__.object)
| instantiate a = A(x), then use a.sqr() to get x**2
|
| Methods defined here:
|
| __init__(self, x)
|
| sqr(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
因为 help(help)
表示它依赖于 pydoc
,特别是 pydoc.help
.
| Define the builtin 'help'.
| This is a wrapper around pydoc.help (with a twist).
检查源代码,我们发现它依赖于 TextDoc.docclass
来生成 类 的帮助文本。所以我们可以 monkeypatch 该方法以生成我们自己的帮助文本:1
import pydoc
def docclass(self, object, *args, **kwargs):
return pydoc.getdoc(object)
pydoc.TextDoc.docclass = docclass
现在我们只会在帮助文本中得到 __doc__
字符串:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# Help on class Foo in module __main__:
#
# This is the docstring.
1.该语法特定于 Python 2.7,因为 OP 使用相应的标签。虽然它似乎也适用于其他版本的 Python。
您可以对内置的 help
函数进行 monkeypatch 以显示 类 的 __doc__
字符串并返回到原始的 help
以显示其他对象:
import inspect
import pydoc
class Helper(object):
def __init__(self):
self.help = __builtins__.help
def __call__(self, obj):
if inspect.isclass(obj):
return pydoc.pager(obj.__doc__)
else:
return self.help(obj)
__builtins__.help = Helper()
产生以下输出:
class Foo:
"""This is the docstring."""
def foo(self):
return None
help(Foo)
# Output:
# This is the docstring.