Trying_hard
Trying_hard

Reputation: 9501

Python Inspect and Functions

I am trying to go through the json module and return just the functions. and then I would like to use inspect module to return inspect.formatargspec(*inspect.getfullargspec(func)) on each function from json.

This is what I was thinking, this obviously fails because func is a string.

import inspect
import json as m 

for func in dir(m):

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

Upvotes: 1

Views: 267

Answers (1)

falsetru
falsetru

Reputation: 369044

dir returns a list of attribute names of the object, not the attributes. You need to use getattr to get the attribute.

import inspect

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

Upvotes: 4

Related Questions