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

前端 未结 3 1679
感动是毒
感动是毒 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:55

    The new syntax is discussed in PEP 3102 and it's indeed not valid in Python 2.x.

    However you can obtain the keyword arguments from **kwargs manually:

    def f(a, *b, **kwargs):
        if 'c' in kwargs:
            pass
    

    The other alternative is to upgrade to Python 3.x.

提交回复
热议问题