如何在 Python 模組中列出所有函式?


在本文中,我們將討論如何列出 Python 模組中的所有函式。

一個 Python 模組 包含多個不同的函式,這些函式允許廣泛的程式碼重用,使複雜程式碼變得簡單。它還透過將平臺相關程式碼更改為平臺無關的 API 來增強 Python 程式的可移植性。

Python 標準庫包含用 C 編寫的模組,這些模組提供對系統功能的訪問,以及用 Python 編寫的模組,這些模組為日常問題提供通用解決方案,使程式設計師的生活變得輕鬆,因為它可以防止為簡單問題編寫冗長的程式碼。

使用 dir() 獲取模組中的函式

Python 的 dir() 函式 用於顯示模組中所有函式和變數的名稱。該函式會產生最相關的結果,而不是完整的結果,因為它列出了公共函式和非公共函式。

示例

以下程式碼給出了使用 dir() 獲取 math 模組的相關函式的示例。

# Importing math module
import math as mt

# Printing all the functions in math module using dir
print(dir(mt))

輸出

輸出顯示了 math 模組中最相關的函式。

['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']

使用 __all__ 獲取模組中的函式

__all__ 提供了所有公共函式的列表,這些函式在使用 import * 時匯入。它提供了所有前面沒有下劃線 (_) 的函式。未定義 __all__ 的模組如果使用者嘗試獲取該模組中的函式,則會丟擲 AttributeError

示例

以下程式碼演示瞭如何使用 __all__ 在 re 模組中顯示不同的函式。

# Importing re module
import re

# Printing different functions in re module
print(re.__all__)

輸出

輸出提供了 re 模組中存在的不同函式。

['match', 'fullmatch', 'search', 'sub', 'subn', 'split', 'findall', 'finditer', 'compile', 'purge', 'template', 'escape', 'error', 'Pattern', 'Match', 'A', 'I', 'L', 'M', 'S', 'X', 'U', 'ASCII', 'IGNORECASE', 'LOCALE', 'MULTILINE', 'DOTALL', 'VERBOSE', 'UNICODE']

使用 inspect 獲取模組中的函式

Python 的 inspect 庫可用於獲取任何模組下的函式。 getmembers() 函式獲取模組內的所有函式和變數,然後 isfunction 過濾器僅顯示函式。與 dir() 不同,使用 inspect 可以顯示模組中的所有函式。

示例

下面給出的程式碼展示了在 inspect 庫中使用 getmembers()isfunction 獲取模組函式的方法。

# Importing getmembers and isfunction from inspect
from inspect import getmembers, isfunction

# Importing math module
import math as mt

# Printing all the functions in math module
print(getmembers(mt), isfunction)

輸出

輸出列出了 math 模組中存在的所有函式。

[('__doc__', 'This module provides access to the mathematical functions\ndefined by the C standard.'), ('__loader__', <class '_frozen_importlib.BuiltinImporter'>), ('__name__', 'math'), ('__package__', ''), ('__spec__', ModuleSpec(name='math', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in')), ('acos', <built-in function acos>), ('acosh', <built-in function acosh>), ('asin', <built-in function asin>), ('asinh', <built-in function asinh>), ('atan', <built-in function atan>), ('atan2', <built-in function atan2>), ('atanh', <built-in function atanh>), ('ceil', <built-in function ceil>), ('comb', <built-in function comb>), ('copysign', <built-in function copysign>), ('cos', <built-in function cos>), ('cosh', <built-in function cosh>), ('degrees', <built-in function degrees>), ('dist', <built-in function dist>), ('e', 2.718281828459045), ('erf', <built-in function erf>), ('erfc', <built-in function erfc>), ('exp', <built-in function exp>), ('expm1', <built-in function expm1>), ('fabs', <built-in function fabs>), ('factorial', <built-in function factorial>), ('floor', <built-in function floor>), ('fmod', <built-in function fmod>), ('frexp', <built-in function frexp>), ('fsum', <built-in function fsum>), ('gamma', <built-in function gamma>), ('gcd', <built-in function gcd>), ('hypot', <built-in function hypot>), ('inf', inf), ('isclose', <built-in function isclose>), ('isfinite', <built-in function isfinite>), ('isinf', <built-in function isinf>), ('isnan', <built-in function isnan>), ('isqrt', <built-in function isqrt>), ('ldexp', <built-in function ldexp>), ('lgamma', <built-in function lgamma>), ('log', <built-in function log>), ('log10', <built-in function log10>), ('log1p', <built-in function log1p>), ('log2', <built-in function log2>), ('modf', <built-in function modf>), ('nan', nan), ('perm', <built-in function perm>), ('pi', 3.141592653589793), ('pow', <builtin function pow>), ('prod', <built-in function prod>), ('radians', <built-in function radians>), ('remainder', <built-in function remainder>), ('sin', <built-in function sin>), ('sinh', <built-in function sinh>), ('sqrt', <builtin function sqrt>), ('tan', <built-in function tan>), ('tanh', <built-in function tanh>), ('tau', 6.283185307179586), ('trunc', <built-in function trunc>)] <function isfunction at 0x7f6c72305940>

更新於: 2023年8月26日

41K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告