2015-07-21 172 views
0

有没有在Python中从某种类型获取模块的方法?我有一个对象,我可以得到该类型,但我无法看到类型的定义。从类型获取模块

+0

相关:http://stackoverflow.com/q/31166088/3001761 – jonrsharpe

回答

1

您可以使用inspect.getmodule检索指定的对象是在定义的模块对象

例如:

>>> import inspect 
>>> from collections import Counter 
>>> c = Counter() 
>>> inspect.getmodule(c) 
<module 'collections' from 'C:\\Program Files\\Python34\\lib\\collections\\__init__.py'> 
>>> inspect.getmodule(Counter) 
<module 'collections' from 'C:\\Program Files\\Python34\\lib\\collections\\__init__.py'> 
+0

在这个特殊的例子,我得到了 BlueTrin

+0

然后它是内置于解释器中的* builtin *模块的一部分。 – poke