我有一个函数,它元组的多个参数,并进行相应的处理。我在想,如果我能在一个for循环传递参数。例如:
def func(*args):
for a in args:
print(f'first {a[0]} then {a[1]} last {a[2]}')
然后,我会打电话的功能
func(('is', 'this', 'idk'), (1,2,3), ('a', '3', 2))
我的问题是,如果有,我可以修改函数调用在一个循环不改变函数定义本身的方式:
func((i, i, i) for i in 'yes'))
例如,它会打印:
first y then y last y
first e then e last e
first s then s last s
是的,在呼叫generator expression和*
argument unpacking:
func(*((i, i, i) for i in 'yes'))
这也可以与分配给一个变量第一发电机表达式写为:
args = ((i, i, i) for i in 'yes')
func(*args)
演示:
>>> func(*((i, i, i) for i in 'yes'))
first y then y last y
first e then e last e
first s then s last s
>>> args = ((i, i, i) for i in 'yes')
>>> func(*args)
first y then y last y
first e then e last e
first s then s last s