如何(用Python方式)检查一个参数是否是Python模块?没有像模块或包这样的类型。
>>> os
<module 'os' from '/usr/lib/python2.6/os.pyc'>
>>> isinstance(os, module)
Traceback (most recent call last):
File "/usr/lib/gedit-2/plugins/pythonconsole/console.py", line 290, in __run
r = eval(command, self.namespace, self.namespace)
File "<string>", line 1, in <module>
NameError: name 'module' is not defined我可以这样做:
>>> type(os)
<type 'module'> 但是我把它比作什么呢?
我已经创建了一个简单的模块来快速查找模块中的方法并获取它们的帮助文本。我为我的方法提供了一个模块变量和一个字符串:
def gethelp(module, sstring):
# here i need to check if module is a module.
for func in listseek(dir(module), sstring):
help(module.__dict__[func])当然,即使module = 'abc':那么dir('abc')会给我字符串对象的方法列表,但我不需要它。
发布于 2009-10-10 09:25:46
from types import ModuleType
isinstance(obj, ModuleType)发布于 2009-10-10 10:13:59
>>> import inspect, os
>>> inspect.ismodule(os)
True发布于 2009-10-10 09:20:44
这看起来有点老生常谈,但是:
>>> import sys
>>> import os
>>> type(os) is type(sys)
Truehttps://stackoverflow.com/questions/1547466
复制相似问题