Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to isinstance(x, module)?

Tags:

python

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.

like image 446
Anton Kazennikov Avatar asked May 14 '09 20:05

Anton Kazennikov


People also ask

What is the Isinstance function?

Python isinstance() Function The isinstance() function returns True if the specified object is of the specified type, otherwise False . If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.

How do you know all the names defined in a module?

The dir() function is used to find out all the names defined in a module. It returns a sorted list of strings containing the names defined in a module. In the output, you can see the names of the functions you defined in the module, add & sub .

How do you check if an object is a function in Python?

The type() function takes in an argument and returns the type of teh object. Apart from this method the isinstance() function can check if an argument is an instance of a particular class or type.


1 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.)

like image 186
RichieHindle Avatar answered Oct 03 '22 17:10

RichieHindle