为什么使用inspect模块无法找到Numpy中的共轭源代码?

时间:2014-02-22 17:25:50

标签: python numpy

我希望看到conjugate中使用的Numpy函数的实现。然后我尝试了以下内容:

import numpy as np
import inspect
inspect.getsource(np.conjugate)

但是,我收到以下错误消息,指出<ufunc 'conjugate'> is not a module, class, method, function, traceback, frame, or code object。愿有人回答原因吗?

In [8]: inspect.getsource(np.conjugate)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-821ecfb71e08> in <module>()
----> 1 inspect.getsource(np.conj)

/Users/duanlx/anaconda/python.app/Contents/lib/python2.7/inspect.pyc in getsource(object)
    699     or code object.  The source code is returned as a single string.  An
    700     IOError is raised if the source code cannot be retrieved."""
--> 701     lines, lnum = getsourcelines(object)
    702     return string.join(lines, '')
    703

/Users/duanlx/anaconda/python.app/Contents/lib/python2.7/inspect.pyc in getsourcelines(object)
    688     original source file the first line of code was found.  An IOError is
    689     raised if the source code cannot be retrieved."""
--> 690     lines, lnum = findsource(object)
    691
    692     if ismodule(object): return lines, 0

/Users/duanlx/anaconda/lib/python2.7/site-packages/IPython/core/ultratb.pyc in findsource(object)
    149     FIXED version with which we monkeypatch the stdlib to work around a bug."""
    150
--> 151     file = getsourcefile(object) or getfile(object)
    152     # If the object is a frame, then trying to get the globals dict from its
    153     # module won't work. Instead, the frame object itself has the globals

/Users/duanlx/anaconda/python.app/Contents/lib/python2.7/inspect.pyc in getsourcefile(object)
    442     Return None if no way can be identified to get the source.
    443     """
--> 444     filename = getfile(object)
    445     if string.lower(filename[-4:]) in ('.pyc', '.pyo'):
    446         filename = filename[:-4] + '.py'

/Users/duanlx/anaconda/python.app/Contents/lib/python2.7/inspect.pyc in getfile(object)
    418         return object.co_filename
    419     raise TypeError('{!r} is not a module, class, method, '
--> 420                     'function, traceback, frame, or code object'.format(object))
    421
    422 ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')

TypeError: <ufunc 'conjugate'> is not a module, class, method, function, traceback, frame, or code object

谢谢!

1 个答案:

答案 0 :(得分:3)

Numpy用C语写,速度很快。您只能看到Python函数的来源。