Python 混合*参数与常规参数
我有以下代码为函数使用*参数Python 混合*参数与常规参数,python,Python,我有以下代码为函数使用*参数 inputs = [fill(input) for input in input_shapes] # num_runs is an integer def bench(num_runs): inputs.append(num_runs) # can I remove this? handle_type(*inputs) del inputs[-1] # can I remove this? 我想知道一种更好的方法来处理*输入,这样我就不
inputs = [fill(input) for input in input_shapes]
# num_runs is an integer
def bench(num_runs):
inputs.append(num_runs) # can I remove this?
handle_type(*inputs)
del inputs[-1] # can I remove this?
我想知道一种更好的方法来处理*输入
,这样我就不必每次都调用append
和del
我尝试了一个简单的例子:
inputs = [1, 2, 3]
inputs2 = [1, 2]
def Test(a, b, c):
print (a, b, c)
Test(*inputs) # works
Test(*inputs2, 3) # SyntaxError: only named arguments may follow *expression
我在这里找到了一个解决方案,这就是我所寻找的 至少在最近的Python 3版本中,您应该能够做到这一点:
inputs=[输入形状中输入的填充(输入)]
def台架(运行次数):
句柄类型(*输入,运行次数)
至少在最近的Python 3版本中,您应该能够做到这一点:
inputs=[输入形状中输入的填充(输入)]
def台架(运行次数):
句柄类型(*输入,运行次数)