Home > Net >  Is there a simple way to pass one or two np.arrays to a function without splatting an array?
Is there a simple way to pass one or two np.arrays to a function without splatting an array?

Time:10-01

I have a function that needs to be able to accept one or two arrays, transform them, and then return the transformed arrays

Is there an alternative to function(*args) that does not unpack a single Numpy array over its first dimension, or a suggested workaround?

CodePudding user response:

Whats wrong with *args? It plays nicely with lists/arrays without splatting.

def foo(*args):
    print(args)

foo([1])
foo([1], [2])

([1],)
([1], [2])

CodePudding user response:

How about using optional args:

def transformsinglearr(arr):
    #enter code here

def foo(arr1 = None, arr2 = None):
    arr1t = None
    arr2t = None
    if not( arr1 is None) : arr1t = transformsinglearr(arr1)
    if not( arr2 is None) : arr2t = transformsinglearr(arr2)
    result = [arr1t, arr2t]
    result = [i for i in result if i] #remove nones
    return result

Its much less elegant than *args but it does the same job.

  • Related