为什么我无法在以下代码中从模块Callable
获得collections
的定义?
如何在模块中获取类的定义?感谢。
>>> from collections import Callable
>>> inspect.getsource(Callable)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/inspect.py", line 944, in getsource
lines, lnum = getsourcelines(object)
File "/usr/lib/python3.5/inspect.py", line 931, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python3.5/inspect.py", line 788, in findsource
raise OSError('could not find class definition')
OSError: could not find class definition
>>> inspect.getsourcelines(Callable)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/inspect.py", line 931, in getsourcelines
lines, lnum = findsource(object)
File "/usr/lib/python3.5/inspect.py", line 788, in findsource
raise OSError('could not find class definition')
OSError: could not find class definition
>>> inspect.getmodule(Callable)
<module 'collections.abc' from '/usr/lib/python3.5/collections/abc.py'>
>>> inspect.getfile(Callable)
'/usr/lib/python3.5/collections/abc.py'
>>> inspect.getsourcefile(Callable)
'/usr/lib/python3.5/collections/abc.py'
答案 0 :(得分:3)
一般来说,这很容易用inspect.getsource
来完成,它接受一个模块,一个类,一个方法,一个函数,一个回溯,一个框架,
或代码对象。它们代表的源当然应该用Python编写,否则会引发错误。
在这种特定情况下,您恰好是不幸的,因为Callable
the Callable.__module__
name is callections.abc
中_collections_abc
已定义:
>>> Callable.__module__
'collections.abc'
这会抛出getsource
,因为它不会查找包含_collections_abc
定义的Callable
,而是collections.abc
只会导入_collections_abc
中的所有定义1}}:
>>> print(getsource(collections.abc))
from _collections_abc import *
from _collections_abc import __all__
通常情况下,getsource
在查找源代码时没有问题,例如:
>>> print(getsource(getsource))
def getsource(object):
"""Return the text of the source code for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a single string. An
OSError is raised if the source code cannot be retrieved."""
lines, lnum = getsourcelines(object)
return ''.join(lines)
在这种特定情况下,确实如此(由于Callable.__module__
返回collections.abc
。)您可以将__module__
替换为'_collections_abc'
,以便查看来源的棘手方法:
>>> Callable.__module__ = '_collections_abc'
>>> src = getsource(Callable)
>>> print(src)
class Callable(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __call__(self, *args, **kwds):
return False
@classmethod
def __subclasshook__(cls, C):
if cls is Callable:
return _check_methods(C, "__call__")
return NotImplemented
但这并不能让我感觉很舒服。
答案 1 :(得分:2)
<强> get_source(fullname)
强>
返回指定模块的源代码。
所以你应该返回整个模块,因为在Callable
的模块中定义了_collections_abc
,所以你的代码应该是这样的:
import _collections_abc
import inspect
print(inspect.getsource(_collections_abc))
您可以在打印结果中看到Callable
的定义。