在 Python 3 中动态传递函数参数

Passing arguments in function dynamically in Python 3

当我调用函数时,python是否支持动态参数传递?

  import itertools.product
  l = [1,2,3,95,5]
  for i in range(5):
      for n in itertools.product(l,l):
         #calculations that
         #reduce set size

我希望通过第 i 个产品的迭代成为:

i=1: product(l,l)

i=2: product(l,l,l)

i=3: product(l,l,l,l)

...

如果我没记错的话,我所知道的唯一支持这种功能的语言是 PHP。

itertools.product 接受一个可选的关键字参数 repeat:

所以,你可以这样做:

for n in itertools.product(l, repeat=i+1):
    ...

或者,要动态传递参数,您可以使用 *args(参见 Unpacking argument lists):

for n in itertools.product(*([l] * (i+1))):
    ...