0
How do I call a random function with arguments from a list?
I'm working on a project, there's a part where multiple functions are inside a list as list elements, so I have to call them randomly from the list Here's an example for the functions without arguments: random.choice(listOfFunctions) #calls a random function But the problem is that some of the functions in this list have an argument and some don't, how do I set a specific value for those that have a argument in case of being called randomly?
2 Respuestas
0
***SOLVED***
My solution: we put functions with arguments and without arguments in two separated lists, we generate two random numbers (for example 0 and 1)
if randint(0,1) == 0:
choice(noArg)()
#empty second parentheses for calling random functions without arguments in noArg list
else:
choice(withArg)(<argument>)
#fill the second parentheses with the argument you want for calling random functions with arguments
+ 3
Mohammad Matin Kateb ,
there might be also possible solutions:
> using function with default argument
> using function with variable-length arguments using *args, (also for no argument)
but this needs a bit more details. the information is a bit sparse, no code example is available.