Choosing a function randomly

from random import choice
random_function_selector = [foo, foobar, fudge]

print choice(random_function_selector)()

Python functions are first-class objects: you can refer to them by name without calling them, and then invoke them later.

In your original code, you were invoking all three, then choosing randomly among the results. Here we choose a function randomly, then invoke it.

Leave a Comment