python – 函数调用之外的星号

我冒险进入python,我有一个关于星号的问题.我知道它们用于函数调用中的参数,但是我已经看到了在函数卡之外使用它们的代码片段(例如,在5个等级的元组中,将它们解压缩为变量,例如:

第一,*中,最后=成绩

每当我尝试在函数调用的参数中使用此上下文/上下文中的星号时,我在解释器中获得了无效的语法.我在这里错过了什么吗?

最佳答案
Python 3添加了扩展元组解包,支持一个通配符,请参阅PEP 3132

*start, tail = ...
head, *middle, tail =  ...

请参阅assignment statements参考文档:

If the target list contains one target prefixed with an asterisk, called a “starred” target: The object must be a sequence with at least as many items as there are targets in the target list, minus one. The first items of the sequence are assigned, from left to right, to the targets before the starred target. The final items of the sequence are assigned to the targets after the starred target. A list of the remaining items in the sequence is then assigned to the starred target (the list can be empty).

在赋值的左侧(目标列表)中使用星号是Python 2中的语法错误.

转载注明原文:python – 函数调用之外的星号 - 代码日志