asd*_*cap 16 python arguments tuples
在Python中,我写了这个:
bvar=mht.get_value()
temp=self.treemodel.insert(iter,0,(mht,False,*bvar))
Run Code Online (Sandbox Code Playgroud)
我正在尝试bvar将函数调用扩展为参数.但它返回:
File "./unobsoluttreemodel.py", line 65
temp=self.treemodel.insert(iter,0,(mht,False,*bvar))
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
刚刚发生了什么?它应该是对的吗?
Ily*_*rov 37
更新:此行为已在Python 3.5.0中修复,请参阅PEP-0448:
建议在元组,列表,集和字典显示中允许解压缩:
*range(4), 4
# (0, 1, 2, 3, 4)
[*range(4), 4]
# [0, 1, 2, 3, 4]
{*range(4), 4}
# {0, 1, 2, 3, 4}
{'x': 1, **{'y': 2}}
# {'x': 1, 'y': 2}
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 24
如果你想传递最后一个参数作为(mnt, False, bvar[0], bvar[1], ...)你可以使用的元组
temp = self.treemodel.insert(iter, 0, (mht,False)+tuple(bvar) )
Run Code Online (Sandbox Code Playgroud)
扩展调用语法*b只能用于Python 3.x上的调用函数,函数参数和元组解包.
>>> def f(a, b, *c): print(a, b, c)
...
>>> x, *y = range(6)
>>> f(*y)
1 2 (3, 4, 5)
Run Code Online (Sandbox Code Playgroud)
元组文字不在其中一种情况下,因此会导致语法错误.
>>> (1, *y)
File "<stdin>", line 1
SyntaxError: can use starred expression only as assignment target
Run Code Online (Sandbox Code Playgroud)