最佳答案
当“解构”一个元组时,我可以使用 _
来表示我不感兴趣的元组元素,例如。
>>> a,_,_ = (1,2,3)
>>> a
1
使用 Python 2.x,我如何用函数参数表达相同的意思呢:
>>> def f(a,_,_): return a
...
File "<stdin>", line 1
SyntaxError: duplicate argument '_' in function definition
我还试图完全省略这个论点:
>>> def f(a,,): return a
File "<stdin>", line 1
def f(a,,): return a
^
SyntaxError: invalid syntax
还有其他方法达到同样的效果吗?