- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下python2.6中的代码抛出语法错误
>>> def f(a,*args,c):
File "<stdin>", line 1
def f(a,*args,c):
^
SyntaxError: invalid syntax
但是这个语法在python3.0中是有效的。我想知道我应该在我的解释器中导入什么才能使其工作。IE。 从导入 __future__ ????
为了导入 3.0 的 print function
,我会做 from __future__ import print_function
同样这个定义在2.6中是无效的
def f(a,*b,c=5,**kwargs):
虽然它在 3.0 中是合法的
最佳答案
Python 3 编译器的这个特性没有被反向移植到 Python 2.x。
没有神奇的 from __future__ import
开关来启用它,您唯一的选择是升级到 Python 3.x。
您的第二个函数可以定义为:
def (a, *b, **kwargs):
c = kwargs.pop('c', 5)
与 Python 2 兼容。
关于python - 如何为 python 3.0 的仅关键字参数导入 __future__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19690483/
如果“print”没有被列为方法之一,这样说是否正确 __future__.__dict__.keys() 那么我使用的Python版本不提供 future 的打印功能? (我使用的是 Python
我一直对 __future__ 着迷模块——特别是,它能够改变语句在 python 中的解析方式。 最有趣的是如何做类似的事情 from __future__ import print_functio
在 python 2.7 中,通过使用 from __future__ import division, print_function 我现在可以让 print(1/2) 显示 0.5。 但是是否可
Python doc __future__ 在 python 文档中关于 __future__下表显示了 3.7.0b1 中的“可选”和“4.0 中的强制性”注释 但是我仍然可以在 3.8.2 中使用
这有效:(结果 = 0.01) from __future__ import division def division_test(): print 10/1000 division_test
使用 Cython header 指令和使用 future 导入的正确方法是什么? Python 2.7.x 例子: 1: from __future__ import division 2: #cy
我可以放置: from __future__ import absolute_import 在我的包的顶层目录 __init__.py 中,并保证 absolute_import 将应用于在该包或子包
运行语句时 from __future__ import annotations 我收到以下错误: Traceback (most recent call last): File "/usr/li
我正在为一个目前只有 Python 2 的项目贡献代码,以允许它在 Python 3 上运行。我应该输入以下内容吗: from __future__ import (unicode_literals,
我有错误,因为 No module named __future__。我使用 tensorflow,它有 Python2.7。运行程序后,出现如下所示的错误。 import tensorflow Tr
规范:Python 2.7 我正在开发一个包含多个模块的项目,我想在所有模块中激活 __future__ 模块的一些功能。我想在一个模块上导入我需要的所有功能,然后将该单个模块导入到每个其他模块,并让
我已经回答了有关future 模块如何工作的问题。 What is __future__ in Python used for and how/when to use it, and how it w
我想在 exec 函数中使用 future 模块。但看起来只在当前的 exec 函数中生效,而不会在后面的 exec 调用中生效。以下代码显示了问题。第二个 future 模块生效,您可以看到输出 h
所有类都需要先定义才能用作类型提示。为了在某些情况下解决这个问题,__future__ import is recommended 。这就是为什么以下代码可以正常工作(在 Python 3.7 中
我个人不明白为什么 from __future__ 导入必须在文件的顶部。我要问的是为什么,为什么他们必须在顶部?这是什么原因? 最佳答案 它们可以更改语言语法,包括但不限于 import 语句的行为
我不是专业程序员,所以我的知识有很多漏洞(请注意)。 我在 python 3.7 中编写了一些库。但现在我想从使用 python 2.7 的 3D 应用程序访问它们。他们拥有的唯一真正的 3.7 特定
这是我的应用程序中已有的其他导入 import os import sys from google.appengine.ext.webapp import template import cgi im
我的 python 脚本开始于 from __future__ import division 在 RI 我做 library(rPython) python.load("myscript.py")
在 python 2.x 中,两个整数相除返回一个整数。但是,如果您使用 from ___future___ import division 你可以获得一个浮点值: >>> 3/2 1 >>> fro
我编写了以下 doctest x.doctest: This is something: >>> x = 3 + 4 foo bar something else: >>> from
我是一名优秀的程序员,十分优秀!