For the pymxs equivalent of this topic, see Performing Inspection of Classes and Objects in pymxs.
The demoClassesInMaxPlus.py
example demonstrates how to obtain the list of classes in the MaxPlus Python API, and the corresponding number of API elements for each class.
The inspect
module provides many useful functions to obtain information about modules, classes, methods, functions and so forth. In this example, the inspect.getmembers()
function is used.
To use the inspect module, you must first:
import inspect
You can use the inspect.getmembers()
function with an optional predicate. If the optional predicate argument is used, then only members for which the predicate argument returns true are returned.
Using this method, you can easily obtain a list of class members by setting your predicate argument to isclass
:
classes = inspect.getmembers(MaxPlus, inspect.isclass)
The \__dict_\_
property is a dictionary that represents the attributes of an object. Each item is a key:value pair where the first value is the name of the attribute and the second is the value of the attribute. In this example, we use the \__dict_\_
to obtain the name of the class and count its number of corresponding API elements:
totalcnt = 0
for c in classes:
name = str(c[0])
membercnt = len(c[1].__dict__)
totalcnt += membercnt
api[name] = membercnt