如何为python 3.0的仅关键字参数导入__future__? [英] how to import __future__ for keyword-only argument of python 3.0?

查看:28
本文介绍了如何为python 3.0的仅关键字参数导入__future__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python2.6以下代码抛出语法错误

<预><代码>>>>def f(a,*args,c):文件<stdin>",第 1 行def f(a,*args,c):^语法错误:无效语法

但是这个语法在python3.0中是有效的.我想知道我应该在解释器中导入什么才能使其工作.IE.from import __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 兼容.

The following code in python2.6 throws syntax error

>>> def f(a,*args,c):
  File "<stdin>", line 1
    def f(a,*args,c):
                  ^
SyntaxError: invalid syntax

but this syntax is valid in python3.0. I would like to know what should I import in my interpreter to make it work. ie. from import __future__ ????

for importing print function of 3.0, I would do from __future__ import print_function

similarly this defination is invalid in 2.6

def f(a,*b,c=5,**kwargs):

while it is legal in 3.0

解决方案

This feature of the Python 3 compiler has not been backported to Python 2.x.

There is no magic from __future__ import switch to enable it, your only option is to upgrade to Python 3.x.

Your second function could instead be defined as:

def (a, *b, **kwargs):
   c = kwargs.pop('c', 5)

to be Python 2 compatible.

这篇关于如何为python 3.0的仅关键字参数导入__future__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆