how to import __future__ for keyword-only argument of python 3.0?

前端 未结 3 1650
感动是毒
感动是毒 2021-01-17 19:58

The following code in python2.6 throws syntax error

>>> def f(a,*args,c):
  File \"\", line 1
    def f(a,*args,c):
                  ^         


        
3条回答
  •  臣服心动
    2021-01-17 20:56

    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.

提交回复
热议问题