![](/img/trans.png)
[英]Python: How to import all methods and attributes from a module dynamically
[英]Displaying python documentation of all methods and attributes with a certain word in a module
我正在嘗試構建一個幫助系統,我想從一個包含一個特定單詞的Python
模塊中依次打印所有方法文檔。 這用於具有大量方法或屬性的模塊中。 現在,這就是我正在做的事情,例如,對於諸如Tkinter.Entry之類的模塊,尋找其中包含“設置”的方法:
q = []
for i in dir(Tkinter.Entry):
if 'set' in i:
q.append(i)
for item in q:
help(item)
對於每種方法,這將顯示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}}
的方式來獲得幫助文本,我就會盡力弄清楚其余部分。 謝謝。
您可以嘗試.__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)
不能與簡單的語句一起使用 。 這與.__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'>
>>>
其中包含所有內容的所有文檔,包括簡單的語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.