At the end of a module, I want to get the classes defined in the module and add some attributes to the class. (The attribute values are calculated based on other class attributes; I want to avoid repeating code in every class definition.) I've found guidance here on how to get a list of classes defined in the module.
classmembers = inspect.getmembers(
sys.modules[__name__],
lambda member: inspect.isclass(member) and member.__module__ == __name__
)
But if I have defined an Enum subclass in the module, I want to filter that out. I tried the following, but it didn't work:
classmembers = inspect.getmembers(
sys.modules[__name__],
lambda member: inspect.isclass(member) and member.__module__ == __name__
and not isinstance(member, Enum)
)
I'm temporarily checking the resulting list with this
for c in classmembers:
print(c)
and enums continue to appear.
...
('VarFWord', <class 'table_COLR_new.VarFWord'>)
('VarFixed', <class 'table_COLR_new.VarFixed'>)
('VarUFWord', <class 'table_COLR_new.VarUFWord'>)
('extend', <enum 'extend'>)
Why is that happening? What do I need to change to filter out the enum?