I need to check if a variable is a module or not. How to do it in the cleanest way?
I need this to initialize some dispatcher function and I want the function to be able to accept either a dict or a module as an argument.
>>> import os, types >>> isinstance(os, types.ModuleType) True
(It also works for your own Python modules as well as built-in ones like os .)
os
I like to use this, so you don't need to import the types module:
isinstance(amodule, __builtins__.__class__)