I am currently writing a python script to display all name of all the python files in a package and also all the name of the class a file contains.
scenario
#A.py
class apple:
.
.
class banana:
.
.
# Extracting_FilesName_className.py
f=open("c:/package/A.py','r')
className=[]
"Here i need to use some command to get all the class name within the file A.py and append it to className'
print file_name,className
out put A.py,apple,banana
I can use an old convention way where i could check for each line whether "class" string to is present and retrieve the className. But i want to know is there is a better way to retrieve all the class Name ?
Something like this:
>>> from types import ClassType
>>> import A
>>> classes = [x for x in dir(A) if isinstance(getattr(A, x), ClassType)]
Another alternative as @user2033511 suggested:
>>> from inspect import isclass
>>> classes = [x for x in dir(A) if isclass(getattr(A, x))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With