在沒有官方文檔的情況下,如何檢查Python package的特定function有哪些arguments?

Yanwei Liu
Jul 2, 2022

對多數的package來說,開發者會針對function的每個argument提供詳細的文字說明,以方便使用者。

但有時新舊版本的同個function,可能會因為版本的更動,有些argument就被移除或是新增。

若官方直接更新文檔,而將舊版的移除,這時,要知道舊版本的function存在哪些argument就變的相當麻煩。

所幸inspect這個套件提供了getargspec這個功能,在不查詢文檔的情況下,也能讓使用者知道某個function具備哪些argument。

# https://www.geeksforgeeks.org/how-to-get-list-of-parameters-name-from-a-function-in-python/import inspect
from pytorch_metric_learning import samplers
print(inspect.getargspec(samplers.MPerClassSampler))
# output
ArgSpec(args=[‘self’, ‘labels’, ‘m’, ‘length_before_new_iter’], varargs=None, keywords=None, defaults=(100000,))

--

--