简体   繁体   English

如何实例(x,模块)?

[英]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. 我需要这个来初始化一些调度函数,我希望函数可以接受dict或模块作为参数。

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

(It also works for your own Python modules, as well as built-in ones like os .) (它也适用于您自己的Python模块,以及像os这样的内置模块。)

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

isinstance(amodule, __builtins__.__class__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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