为什么我看不到从 future 导入的打印函数的参数? [英] Why can't I see the arguments to the print function imported from future?
问题描述
我注意到在使用 from __future__ import print_function
导入新的 python 函数后,我无法使用它.在我发现原因的旅程中,我什至无法检查它需要哪些参数/参数.为什么?
I noticed that I cannot use flush with the new python function after I import it using from __future__ import print_function
. In my journey to discover why I discovered I can't even inspect what arguments/parameters it takes. Why is it?
- 首先,我确保检查功能有效.
- 然后我确定打印函数确实是一个函数.
- 在这两个(似乎通过/检查)之后,我尝试检查它,但失败并返回一个奇怪的错误.
这是我所做的:
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)
除了检查打印外,一切都通过了:
and everything passed except inspecting print:
ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=(1,))
what is print? <built-in function print>
what is g=print? <built-in function print>
Traceback (most recent call last):
File "print_future.py", line 16, in <module>
print( inspect.getargspec( g ) )
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 816, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <built-in function print> is not a Python function
为什么会这样?
这是我的python和系统的一些信息:
This is some info of my python and system:
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.
>>>
推荐答案
注意
某些可调用对象在某些实现中可能无法自省Python.例如,在 CPython 中,C 中定义的内置函数不提供有关其参数的元数据.
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 起已弃用
I've posted the signature docs because inspect.getargspec is deprecated since 3.0
这篇关于为什么我看不到从 future 导入的打印函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!