是否可以从 builtin_function_or_method 中检索 func_code 对象? 即从 time.time()
import time
dir(time.time)
不包含函数对象
也没有
dir(time.time.__call__)
它自己返回
time.time.__call__.__call__.__call__
..等等。
有什么想法吗?
最佳答案
在 CPython 中,内置方法是用 C(或其他一些语言,例如 C++)实现的,因此不可能获得 func_code
(该属性仅存在于使用 Python 定义的函数中).
您可以在此处找到time.time
的源代码:http://hg.python.org/cpython/file/v2.7.5/Modules/timemodule.c#l126
其他 Python 实现可能使 func_code
可用于内置函数。例如,在 PyPy 上:
$ pypy
Python 2.7.1 (7773f8fc4223, Nov 18 2011, 22:15:49)
[PyPy 1.7.0 with GCC 4.0.1] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> import time
>>>> time.time
<built-in function time>
>>>> time.time.func_code
<builtin-code object at 0x00000001017422e0>
>>>> time.time.func_code.co_consts
('time() -> floating point number\n\n Return the current time in seconds since the Epoch.\n Fractions of a second may be present if the system clock provides them.',)
关于python - 从 builtin_function_or_method 检索 func_code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18421666/