How to check if an attribute is a function/class?

Question:
I’m looping through every attribute from a file and printing the attribute’s docstring. I only want to print the docstrings of functions/classes, though. How can I check if the attribute is a class or function?

Repl link:
https://replit.com/@QwertyQwerty54/ExpensiveGleamingEvaluations

Code Snippet:

print(test.__doc__)
for attrName in dir(test):
    print(getattr(test, attrName).__doc__)

Function check:

callable(function_name)

Class check:

import inspect
inspect.isClass(class_name)

Note: From Google searches for those checks.

Attempted implementation:

from inspect import isClass
print(test.__doc__)
for attrName in dir(test):
    attribute = getattr(test, attrName)
    if callable(attribute) or isClass(attribute):
        print(attribute.__doc__)
3 Likes

isClass gave an error but classes are callable too so it worked without that! Thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.