tags:

views:

129

answers:

2

Hello!

I need to test if a variable is a module or not. How to do this in the cleanest way?

I need this for initializing some dispatcher function and I want that the function can accept either dict or module as an argument.

+12  A: 
>>> import os, types
>>> isinstance(os, types.ModuleType)
True

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

RichieHindle
Thanks! It works perfectly!
Anton Kazennikov
+3  A: 

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

isinstance(amodule, __builtins__.__class__)
Unknown