python - 从 builtin_function_or_method 检索 func_code

标签 python python-2.7

是否可以从 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/

相关文章:

python - 3D 轴上填充等高线图中的伪影

python - 如何删除包含不同顺序元素的重复列表?

python-2.7 - 'requests[0].delete_dimension.range.sheet_id' (TYPE_INT32) 处的值无效

python-2.7 - 我如何从本应在命令行上运行的脚本调用Python函数

python - groupby 在搜索 View 中的多对多关系字段上?

python 滚动解决方法

python - 使用 Python xlwt 模块编写 HTML

python - 使用自动重建索引将 Series 插入 DataFrame

python - 使用日志记录将消息记录到数组/列表

python - 无法安装 MySQL-python [CentOS]