【问题标题】:Getting parameter names for __future__ functions in python在 python 中获取 __future__ 函数的参数名称
【发布时间】:2014-11-06 17:53:40
【问题描述】:

我的__future__.print_function 2.7.5 版本不允许使用new 参数:

>>> print('hi', end='')
Parsing error SyntaxError: invalid syntax (line 1)

如果我想不通,我会问为什么这会在单独的帖子中。现在,我想看看我的环境版本的这个函数有哪些参数可用。

我查看了this SO post 和一些相关的,但是当我尝试时这些似乎不起作用:

>>> print.func_code.co_varnames
Parsing error SyntaxError: invalid syntax (line 1)
>>> print_function.func_code.co_varnames
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: _Feature instance has no attribute 'func_code'

我猜__future__ 函数的特殊性质是这种标准技术失败的原因。

还有其他方法可以检查我的版本 __future__.print_function 采用什么参数?

【问题讨论】:

  • 你使用什么 REPL 导致解析错误?
  • 另外,3.x 中的print() 也不支持new
  • 谢谢。这与 Martijn 的信息一致。不确定“REPL”是什么,但这是 Python 2.7.5。

标签: python


【解决方案1】:

您试图将内置函数(在 C 中实现)视为用户定义的函数。它们不是同一件事。 .func_code 仅为用户定义的函数定义(在 Python 中实现)。

__future__ module 仅包含有关功能的元数据,__future__.print_function 对象与print() 函数不是同一个对象。相反,该对象会告诉您更多关于哪个版本的 Python 最先支持该功能,以及在哪个版本中该功能成为强制性的(from __future__ 导入变为无操作),以及compile() 的位字段标志功能:

>>> import __future__
>>> __future__.print_function
_Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 65536)
>>> __future__.print_function.optional
(2, 6, 0, 'alpha', 2)
>>> __future__.print_function.mandatory
(3, 0, 0, 'alpha', 0)
>>> __future__.print_function.compiler_flag
65536

在 Python 2.7 中,print() 等内置函数对象根本没有足够的信息让您发现它们支持哪些参数。在 Python 3 中,这正在慢慢改变为 more and more built-in types are given metadata,但 print() 函数尚未在其中:

>>> import inspect
>>> inspect.signature(print)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python3.4/inspect.py", line 2045, in signature
    return _signature_internal(obj)
  File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python3.4/inspect.py", line 1947, in _signature_internal
    skip_bound_arg=skip_bound_arg)
  File "/Users/mpietre/Development/Library/buildout.python/parts/opt/lib/python3.4/inspect.py", line 1884, in _signature_from_builtin
    raise ValueError("no signature found for builtin {!r}".format(func))
ValueError: no signature found for builtin <built-in function print>

我不确定你从哪里得到的想法,newany Python 版本中print() 的有效关键字。不存在支持该论点的 Python 版本。

在 Python 2 中的 print() 中缺少并且在 Python 3.3 及更高版本中存在的唯一参数是 flush 参数,请参阅 Python 3 docs for print()

[...] 如果 flush 关键字参数为真,则流被强制刷新。

3.3 版更改:添加了 flush 关键字参数。

对此进行测试的唯一方法(使用sys.version_info &gt;= (3, 3) 测试除外)是尝试使用它:

from io import StringIO

try:
    print('', end='', flush=False, file=StringIO())
    print_supports_flush = True
except TypeError:
    print_supports_flush = False

【讨论】:

  • 太棒了。感谢所有关于 future 函数的背景知识...我从这篇文章中得到了“新”论点:stackoverflow.com/questions/493386/…。我误会了吗?
  • @Roland:我想你做到了;那里没有提到 new 参数,只是 print() 函数是新的。
  • 嗯,我正在查看 CodeLogic 答案中的第三个示例。它在谈论 Python 3.x(这可能是我的问题)。
  • 然而他的例子都没有使用new 参数。他的最后一个示例确实使用了flush,它仅适用于 Python 3.3 及更高版本。
  • print('.', end="") 怎么样?它就在flush 示例之前。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2018-07-27
  • 2010-10-09
相关资源
最近更新 更多