I want to store functions in a list and then later in a program call those functions from that list with values also stored on that list.
Example:
import random
import time
ranges = 23,24
my_functions_and_values = [[random.randint, ranges], [time.sleep, 2]]
for i in my_functions_and_values:
i[0](i[1])
But it gives me the following error:
TypeError: randint() missing 1 required positional argument: 'b'
You can store the parameters to functions as tuples (so 2
will become (2, )
). Then when you call the function do parameter unpacking with *
:
import random
import time
ranges = 23,24
my_functions_and_values = [[random.randint, ranges], [time.sleep, (2, )]]
for i in my_functions_and_values:
i[0](*i[1])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments