[python] Get the list of classes defined in the module

inspect.getmembers(object[, typecheck])

First from the conclusion

foo.py


class foo1:
    '''something code'''

class foo2:
    '''something code'''

class foo3:
    '''something code'''

get_class


import foo 
import inspect

classes = map(lambda x:x[0],inspect.getmembers(foo,inspect.isclass))

print classes
# => ['foo1','foo2','foo3']

ʻInspect.getmembers (object) gets all the members of the object registered in ʻobject. The format is [(member1, type), (member2, type), ....]. ʻInspect.is--determines if the object matches--` It seems that there are 16 classes, modules, functions, method ..... For more information, click here (http://docs.python.jp/2/library/inspect.html "inspect").

Therefore, ʻinspect.getmembers (foo, inspect.isclass)` should have been like this.

get_class


print inspect.getmembers(foo,inspect.isclass)
# => [('foo1',<class foo.foo1 at 0x*******>),('foo1',<class foo.foo2 at 0x*******>),('foo3',<class foo.foo3 at 0x*******>)]

After that, you only have to extract the 0th element of each element. map(lambda x:x[0], list) That's it.

Since the first element is the class itself, you can also get the class object itself. For example, suppose you search key for a part of the class name and get a matching class object.

getclass


import foo 
import inspect

classes = inspect.getmembers(foo,inspect.isclass)

for i in classes:
    if '2' in i[0]:
        something2 = i[1]

something2class = something2()
print something2class.__class__.__name__
# => foo2

However, in this case, if you do not find it, you will probably get an error.

Recommended Posts

[python] Get the list of classes defined in the module
Get the number of specific elements in a python list
Get the EDINET code list in Python
[python] Get the rank of the values in List in ascending / descending order
[Python] Sort the list of pathlib.Path in natural sort
Get the caller of a function in Python
Make a copy of the list in Python
How to get the number of digits in Python
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
[Python] Outputs all combinations of elements in the list
Get the URL of the HTTP redirect destination in Python
How to get a list of files in the same directory with python
Get the desktop path in Python
Get the script path in Python
Get the desktop path in Python
Get the host name in Python
About the basics list of Python basics
Master the weakref module in Python
Try to get the function list of Python> os package
How to get the last (last) value in a list in Python
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Get the number of occurrences for each element in the list
Get the index of each element of the confusion matrix in Python
Get index of nth largest / smallest value in list in Python
2015-11-26 python> Display the function list of the module> import math> dir (math)
Get the value of a specific key in a list from the dictionary type in the list with Python
Check the behavior of destructor in Python
Try to get a list of breaking news threads in Python.
Pass the path of the imported python module
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Get the value of a specific key up to the specified index in the dictionary list in Python
OR the List in Python (zip function)
The result of installing python in Anaconda
Check the path of the Python imported module
[python] Get a list of instance variables
The basics of running NoxPlayer in Python
Summary of built-in methods in Python list
In search of the fastest FizzBuzz in Python
[Python] Get the character code of the file
[Python] Get a list of folders only
Install the Python module in any directory
Get rid of DICOM images in Python
Get a list of files in a folder with python without a path
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Get a capture of the entire web page in Selenium Python VBA
If you want a singleton in python, think of the module as a singleton
Get a list of packages installed in your current environment with python
[Linux] Command to get a list of commands executed in the past
Receive a list of the results of parallel processing in Python with starmap
Get a datetime instance at any time of the day in Python
Get the key for the second layer migration of JSON data in python
Get the contents of git diff from python
Output the number of CPU cores in Python
[python] Check the elements of the list all, any
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
[Python] Get / edit the scale label of the figure