1

I want to error-test several functions in my library by passing None to all their input arguments:

fun_1(None)
fun_2(None, None)
fun_3(None)
fun_4(None, None, None)

However I have a lot of functions and I want to make it very simple to add each of the to the test.

I want to make a list of functions as:

my_list= [fun_1, fun_2, fun_3, fun_4]

And then being able to call all of them with all their arguments as None:

for f in my_list:
    f(#I don't know what to put here !)

Is there a syntax that allows this?

PS. in this particular lib passing None is always an error.

Ivan
  • 1,352
  • 2
  • 13
  • 31
  • Do your functions always have only positional arguments, or do some have keyword-only arguments? – kaya3 Mar 27 '23 at 09:12
  • @kaya3 they may have both, but I'm interested on why you ask this :) – Ivan Mar 27 '23 at 09:18
  • Because if they are only positional arguments, then it's only required to count how many `None` values you want to pass to the function, but if there are keyword-only arguments too then it is necessary to look at the individual argument names. – kaya3 Mar 27 '23 at 09:19

1 Answers1

3

I made this answer with this post and this post.

What you could do is create a wrapper function, like in the second post, to call your function. You can know how many arguments each function needs with the method shown in the first one.

I hope this is what you asked for.

def wrapper2(func, args):
    func(*args)


def fun_1(x):
    print(x)


def fun_2(x, y):
    print("I love democracy")


def fun_3(x):
    print(type(x))


def fun_4(x, y, z):
    print(f"received {x}, {y}, {z}")


my_list = [fun_1, fun_2, fun_3, fun_4]


for f in my_list:
    how_many_args = f.__code__.co_argcount
    args_list = []
    for i in range(how_many_args):
        args_list.append(None)

    wrapper2(f, args_list)

Output is:

None
I love democracy
<class 'NoneType'>
received None, None, None
TCH
  • 78
  • 6