osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Get Count of function arguments passed in


On Wednesday, 11 September 2019 20:25:32 UTC+10, Sayth Renshaw  wrote:
> On Wednesday, 11 September 2019 20:11:21 UTC+10, Sayth Renshaw  wrote:
> > 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
> 
> Tried creating a list of the arguments, however I am creating too many positional arguments.
> 
> matrix1 = [[1, -2], [-3, 4],]
> matrix2 = [[2, -1], [0, -1]]
> matrix3 = [[2, -1], [0, -1]]
> 
> matrix = []
> def add_many(a = list(*matrix)):
>     for num in a:
>         for i in range(len(matrix[num])):
>             for j in range(len(matrix[num])):
>                 print(matrix[num][i][j] + matrix[num][i][j])
> 
> add_many(matrix1,matrix2)

Last failure for the moment

matrix1 = [[1, -2], [-3, 4],]
matrix2 = [[2, -1], [0, -1]]
matrix3 = [[2, -1], [0, -1]]

matrix = []
def add_many(*matrix):
    for num in range(add_many().func_code.co_argcount):
        for i in range(len(matrix[num])):
            for j in range(len(matrix[num])):
                print(matrix[i][j] + matrix[i][j])

add_many(matrix1,matrix2)

Sayth