how to use inspect to get class name in python in python

You can use the inspect module to get the class name in Python:

main.py
import inspect

class MyClass:
    pass

print(inspect.getmembers(MyClass, inspect.isclass)[0][0])
99 chars
7 lines

This code will output 'MyClass'. It uses the getmembers function from the inspect module to get all the members of the MyClass object that are classes, and then takes the first one (which in this case is the only one) and retrieves its name.

related categories

gistlibby LogSnag