Search code examples
pythonarraysnumpyiteratorargs

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


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?


Solution

  • 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.