【问题标题】:Displaying python documentation of all methods and attributes with a certain word in a module显示模块中带有特定单词的所有方法和属性的python文档
【发布时间】:2014-08-03 01:41:58
【问题描述】:

我正在尝试构建一个帮助系统,并且我想从包含某个单词的Python 模块中打印所有方法文档,一个接一个。这适用于具有大量方法或属性的模块。现在这就是我正在做的事情,例如,对于诸如 Tkinter.Entry 之类的模块,寻找其中包含例如“设置”的方法:

q = []
for i in dir(Tkinter.Entry):
    if 'set' in i:
       q.append(i)
for item in q:
    help(item)

这显示了每个方法的 7 个 pydoc 项,名称中一个接一个地带有“set”,并在解释器上干净地退出。但是,我似乎无法在作业中获取 pydoc,例如:

x = help(item)   # doesn't work.
# handle display code here.

我在这里缺少什么?

以下是上下文,不一定是问题的一部分(例如):

是否有一种解决方法可以获得与此相同的效果,例如,一个简单的html,每次用户单击时都会显示下一个帮助项目,“关闭”当前元素?

<html>
  <head> 
    Help page 
  </head>
  <script type="text/javascript">
    $ function closediv(){
      <!-- code to close the div goes here-->
      }
  </script>
  <body>
    <!--maybe a list of divs here-->
    <div id="help text">{% block content %} {{x}} {% endblock %}</div>
  </body>
 </html>

只要我能找到一种方法,以一种可以传递给块内容{{X}} 的方式获取帮助文本,我会尝试找出其余的。谢谢。

【问题讨论】:

    标签: python helpers pydoc


    【解决方案1】:

    您可以尝试.__doc__ 来很好地理解一些关键术语:

    >>> print pow.__doc__
    pow(x, y[, z]) -> number
    
    With two arguments, equivalent to x**y.  With three arguments,
    equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
    >>> print list.__doc__
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    >>> 
    

    help(item) 不能与 simple statements 一起使用。这和.__doc__一样:

    >>> help(if)
      File "<stdin>", line 1
        help(if)
              ^
    SyntaxError: invalid syntax
    >>> if.__doc__
      File "<stdin>", line 1
        if.__doc__
          ^
    SyntaxError: invalid syntax
    >>> 
    

    我建议查看pydoc.py:

    >>> import pydoc
    >>> pydoc
    <module 'pydoc' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc'>
    >>> 
    

    其中包含所有内容的所有文档,包括简单的语句。

    【讨论】:

    • 谢谢,似乎有一个方法 pydoc.getdoc 返回 pydoc 的字符串版本。 x = pydoc.getdoc(Tkinter.Entry)x'Entry widget which allows to display simple text.'
    • @CoriolisForce 如果这个答案对你有帮助,你介意accepting it 点击我的答案旁边的绿色对勾吗?全面披露:它给 +2声誉:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2013-09-12
    • 2020-10-05
    • 2020-07-20
    • 1970-01-01
    • 2019-01-28
    • 2013-06-26
    相关资源
    最近更新 更多