Invalid syntax python starred expressions

前端 未结 3 1886
我在风中等你
我在风中等你 2020-12-16 16:20

I am trying to unpack set of phone numbers from a sequence, python shell in turn throws an invalid syntax error. I am using python 2.7.1. Here is the snippet



        
3条回答
  •  孤城傲影
    2020-12-16 17:08

    This new syntax was introduced in Python 3. So, it'll raise error in Python 2.

    Related PEP: PEP 3132 -- Extended Iterable Unpacking

    name, email, *phone_numbers = user_record
    

    Python 3:

    >>> a, b, *c = range(10)
    >>> a
    0
    >>> b
    1
    >>> c
    [2, 3, 4, 5, 6, 7, 8, 9]
    

    Python 2:

    >>> a, b, *c = range(10)
      File "", line 1
        a,b,*c = range(10)
            ^
    SyntaxError: invalid syntax
    >>> 
    

提交回复
热议问题