如何查看函数属于哪个模块? 喜欢,
check_module(sqrt)
将返回 math 等,如果有的话。
请您参考如下方法:
函数有一个 __module__ 属性:
>>> from math import sqrt
>>> sqrt.__module__
'math'
您可以使用 inspect.getmodule() function获取给定对象的实际模块对象:
>>> from inspect import getmodule
>>> getmodule(sqrt)
<module 'math' from '/Users/mj/Development/Library/buildout.python/python-3.4/lib/python3.4/lib-dynload/math.so'>
inspect.getmodule() 不仅仅适用于函数和类;根据给定对象的元数据,它会经过一段时间才能找到该对象的模块。
