[Tutor] random.choice()

Tim Golden mail at timgolden.me.uk
Tue Jul 1 14:43:22 CEST 2008

Dick Moores wrote:
> So I want to randomly choose between them. I thought that I might be 
> able to use choice() to do that. So,
>      (bunch of functions here)
> if __name__ == '__main__':
>     choice([use_for_float_demo(), use_for_integer_demo()])   
> I find that what's gets run each time is BOTH of these functions, first 
> use_for_float_demo(), and then use_for_integer_demo()!  What's going on?

What you *want* to do is choose one of two functions, and call
whichever is chosen:

fn = choice ([a, b])
result = fn ()

What you're *actually* doing is calling two functions, and returning
one result or the other:

result = choice ([a (), b ()])

More information about the Tutor mailing list