0

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'

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.