Get Count of function arguments passed in

ast none at gmail.com
Wed Sep 11 07:12:55 EDT 2019


Le 11/09/2019 à 12:11, Sayth Renshaw a écrit :
> Hi
> 
> I want to allow as many lists as needed to be passed into a function.
> But how can I determine how many lists have been passed in?
> 
> I expected this to return 3 but it only returned 1.
> 
> matrix1 = [[1, -2], [-3, 4],]
> matrix2 = [[2, -1], [0, -1]]
> matrix3 = [[2, -1], [0, -1]]
> # print(add(matrix1, matrix2))
> 
> def add(*matrix):
>      print(len(locals()))
> 
> add(matrix1,matrix2,matrix3)
> 
> Cheers
> 
> Sayth
> 

It returns 1 because there is only 1 local variable
inside function add. It's a list matrix which contains
the 3 matrix

If you want the number of arguments passed, then
just call: len(matrix)

def add(*matrix):
     print(len(matrix))






More information about the Python-list mailing list