Python 检查和功能

Python Inspect and Functions

我正在尝试通过 json 模块和 return 只是功能。然后我想在 json.

的每个函数上使用检查模块 return inspect.formatargspec(*inspect.getfullargspec(func))

这就是我的想法,这显然失败了,因为 func 是一个字符串。

import inspect
import json as m 

for func in dir(m):

    if inspect.isfunction(func):
        print(func)

dir returns a list of attribute names of the object, not the attributes. You need to use getattr获取属性。

import inspect

for func in dir(m):  # `func`: str
    if inspect.isfunction(getattr(m, func)):  # <----
        print(func)