从类型中获取模块
Getting Module from Type
Python 中是否有从类型中获取模块的方法?我有一个可以获取类型的对象,但看不到定义类型的位置。
您可以使用 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'>
Python 中是否有从类型中获取模块的方法?我有一个可以获取类型的对象,但看不到定义类型的位置。
您可以使用 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'>