Choosing a function randomly

Solution 1:

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.

Solution 2:

from random import choice

#Step 1: define some functions
def foo(): 
    pass

def bar():
    pass

def baz():
    pass

#Step 2: pack your functions into a list.  
# **DO NOT CALL THEM HERE**, if you call them here, 
#(as you have in your example) you'll be randomly 
#selecting from the *return values* of the functions
funcs = [foo,bar,baz]

#Step 3: choose one at random (and call it)
random_func = choice(funcs)
random_func()  #<-- call your random function

#Step 4: ... The hypothetical function name should be clear enough ;-)
smile(reason=your_task_is_completed)

Just for fun:

Note that if you really want to define the list of function choices before you actually define the functions, you can do that with an additional layer of indirection (although I do NOT recommend it -- there's no advantage to doing it this way as far as I can see...):

def my_picker():
    return choice([foo,bar,baz])

def foo():
    pass

def bar():
    pass

def baz():
    pass

random_function = my_picker()
result_of_random_function = random_function()

Solution 3:

Almost -- try this instead:

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

print(choice(random_function_selector)())

This assigns the functions themselves into the random_function_selector list, rather than the results of calling those functions. You then get a random function with choice, and call it.