为什么我看不到从future导入的print函数的参数?

2024-07-07 03:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我注意到,在使用from __future__ import print_function导入新的python函数后,我不能对它使用flush。在我的旅程中,我发现我甚至不能检查它需要什么参数。为什么?在

  1. 首先,我确定了inspect函数是有效的。在
  2. 然后我确定print函数确实是一个函数。在
  3. 在这两次(似乎通过/检查)之后,我试图检查它,但这失败了,并返回了一个奇怪的错误。在

以下是我所做的:

from __future__ import print_function

import inspect

def f(a, b=1):
    pass

#print( print_function )
print( inspect.getargspec( f ) )
g = print
print('what is print: ', print)
print('what is g=print: ', g)
print( inspect.getargspec( g ) )
#print( inspect.getargspec( print ) )

#print('Hello', flush=True)

一切都通过了除了检查指纹:

^{pr2}$

为什么会这样?在


这是我的系统的一些信息:

Python 2.7.11 (default, Jun 24 2016, 21:50:11)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Tags: 函数fromimport参数isdef错误function
2条回答

在3.3中,flush关键字被添加到print()中。在

C函数通常不携带内省所需的信息。这只是C如何定义和编译的一个事实。作为替代,签名被添加到它们的docstring中。如果inspect不工作,空闲的调用提示将返回到docstring。在

在3.4中,增加了一个新的机制来包含一个具有C编码功能的签名属性。新的检验人签字在场时使用。一些C编码的函数已经被转换为包含新的属性,而很多没有。在

摘自signature

Note

Some callables may not be introspectable in certain implementations of Python. For example, in CPython, built-in functions defined in C provide no metadata about their arguments.

我已经发布了签名文档,因为inspect.getargspec从3.0开始就不推荐使用了

相关问题 更多 >