Bol*_*ter 4 python arguments tuples typeerror
例如:
mytuple = ("Hello","World")
def printstuff(one,two,three):
print one,two,three
printstuff(mytuple," How are you")
Run Code Online (Sandbox Code Playgroud)
这自然会因为TypeError而崩溃,因为我只想给它两个参数.
是否有一种简单的方法可以有效地"分裂"元组,而不是扩展所有内容?喜欢:
printstuff(mytuple[0],mytuple[1]," How are you")
Run Code Online (Sandbox Code Playgroud)
有点,...你可以这样做:
>>> def fun(a, b, c):
... print(a, b, c)
...
>>> fun(*(1, 2), 3)
File "<stdin>", line 1
SyntaxError: only named arguments may follow *expression
>>> fun(*(1, 2), c=3)
1 2 3
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,只要您使用其名称限定其后面的任何参数,您就可以完成您想要的任务.
归档时间: |
|
查看次数: |
3025 次 |
最近记录: |