How is isinstance (x, module)?

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.

+12


a source to share


2 answers


>>> import os, types
>>> isinstance(os, types.ModuleType)
True

      



(It also works for your own Python modules as well as built-in ones like os

.)

+24


a source


I like to use this, so you don't need to import the types module:



isinstance(amodule, __builtins__.__class__)

      

+4


a source







All Articles