简体   繁体   中英

how to isinstance(x, module)?

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.

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

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

我喜欢使用它,所以你不必导入类型模块:

isinstance(amodule, __builtins__.__class__)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM