如果我有一個名爲「數學」的模塊可以稱爲導入「數學」,那麼我如何獲得所有內部函數的列表與「數學」相關的函數如何訪問python中模塊的所有函數的列表
2
A
回答
4
有一個dir函數,該函數列出了對象的所有(很好,很多)屬性。但是,僅過濾功能是沒有問題的:
>>>import math
>>>dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>>
>>>[f for f in dir(math) if hasattr(getattr(math, f), '__call__')] # filter on functions
['acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
您可能會發現Guide to Python introspection是一個有用的資源,以及這個問題:how to detect whether a python variable is a function?。
3
這就是help()
就派上用場了(如果你喜歡一個人可讀格式):
>>> import math
>>> help(math)
Help on built-in module math:
NAME
math
<snip>
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
acosh(...)
acosh(x)
Return the hyperbolic arc cosine (measured in radians) of x.
asin(...)
<snip>
2
僅適用於內置功能:
from inspect import getmembers, isfunction
functions_list = [o for o in getmembers(my_module, isfunction)]
相關問題
- 1. 如何列出Python模塊中的所有函數?
- 2. 函數方案列表 - 如何訪問列表中的函數?
- 3. 如何訪問列表中的所有數據點中列出
- 4. 我如何獲得所有Python標準庫模塊的列表
- 5. 的Python:如何訪問父模塊
- 6. 的Python:如何訪問列表中的
- 7. require.js:訪問所有加載的模塊
- 8. 如何使用循環訪問列表中的所有列表
- 9. 訪問模塊級變量,從模塊中的函數中
- 10. 如何獲得Python中所有內置函數的列表?
- 11. 幫助訪問Python函數中的模塊變量?
- 12. 有什麼辦法可以在Python中訪問模塊的私有函數嗎?
- 13. 如何從javascript模塊模式中的函數原型訪問私有屬性?
- 14. 如何使用python訪問ibm clearquest?有沒有好的模塊?
- 15. 如何訪問require.js模塊中的閉包函數?
- 16. MS UNITY:如何訪問我的所有模塊的字典?
- 17. 在函數內的Python模塊中導入所有內容
- 18. BeautifulSoup Python我如何返回我的函數列表中的所有值作爲我的函數中的列表
- 19. 訪問Python中列表中的所有值
- 20. 如何訪問有序列表中的所有元素
- 21. 如何拒絕訪問Python模塊
- 22. 如何訪問opencart模塊上的所有前端頁面?
- 23. 如何訪問Python中的字符串列表中的列表
- 24. 無法訪問Canopy中的python模塊?
- 25. 無法從Python 3.5下的導入模塊訪問函數
- 26. for循環不訪問列表中的所有元素python
- 27. RequireJS所有模塊的提取列表
- 28. python:如何訪問函數的屬性
- 29. 如何訪問python列表中的元組內的列表
- 30. Javascript模塊模式 - 所有模塊都可以訪問eachother嗎?
極其有用,在我的情況非常感謝。 – JAZs