1

I would like to pass a tuple of arguments args (at what the tuple varies in length due to some other functions) to a function, which works

def myf(x1,x2,x3,....)
    return something involving x1,x2....

args = (y1,y2,.....)

call myf with args as myf(y1,y2,....)

How do you achieved this? In my actual problem I am working with sympy the the function myf is actually reshape and the variable argument list args is a tuple generated by getting the shape of some n-d-array, say A, so args = A.shape. In the end I want to reshape another array B based on the shape of A. A minimal example ist

from sympy import *
A = Array(symbols('a:2:3:4:2'),(2,3,4,2))
B = Array(symbols('b:8:3:2'),(8,3,2))
args = A.shape
print args
print B.reshape(2,3,4,2) # reshape(2,3,4,2) is the correct way to call it
print B.reshape(args) # This is naturally wrong since reshape((2,3,4,2)) is not the correct way to call reshape
jpp
  • 159,742
  • 34
  • 281
  • 339

2 Answers2

5

Use argument unpacking:

def myf(x1,x2,x3):
    return x1 + x2 + x3

args = (1, 2, 3)

myf(*args)  # 6
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You need to unpack the tuple:

B.reshape(*args)
Phydeaux
  • 2,795
  • 3
  • 17
  • 35