'Passing an unspecified numpy array as an argument to a function

I want to be able to define a function as having an argument of an unspecified array. For example,

import numpy as np
def cols(np.array([]): 
    return len(np.array([])

Say that:

x=np.array([[1,2,3],[4,5,6]])

Then I want cols(x) to give output 3.

Note, the input must be a np.array, so please no workaround to that!



Solution 1:[1]

You don't have to specify what is your argument in python

import numpy as np

def cols(arr):
    return arr.shape[1]

number_of_cols = cols(np.array([[1, 2, 3], [4, 5, 6]]))
print(number_of_cols) # will print 3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Icenore