Invalid syntax python starred expressions

前端 未结 3 1878
我在风中等你
我在风中等你 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:07

    That functionality is only available in Python 3, an alternative is:

    name, email, phone_numbers = record[0], record[1], record[2:]
    

    Or something like:

    >>> def f(name, email, *phone_numbers):
            return name, email, phone_numbers
    
    >>> f(*record)
    ('Dave', 'dave@example.com', ('773-555-1212', '847-555-1212'))
    

    but that is pretty hacky IMO

提交回复
热议问题