Reputation: 575
I am importing a variable number of modules that all need to offer the same functions (func1
, func2
, func3(n)
).
I know all the modules names and need to verify that they all - at least offer the function, without executing them (e.g. not something like try something = mod1.func1 except NotWorking: print("nope")
) or parsing the whole module for the presence of "def func1:
".
How could I verify that these modules offer said functions?
Upvotes: 0
Views: 24
Reputation: 600051
Modules are objects, and methods are just attributes of those objects. You can use the normal getattr
and hasattr
functions:
hasattr(mod1, "func1")
But this sounds like a strange way to do things. It would probably be better to use a class with a proper inheritance mechanism, so that the superclass defines the interface by declaring abstract methods (which can do nothing but raise NotImplementedError
) and the subclasses implement that interface.
Upvotes: 1