running a random function
Brian van den Broek
broek at cc.umanitoba.ca
Thu Jun 7 12:40:31 EDT 2007
David Bear said unto the world upon 06/07/2007 11:56 AM:
> I would like to write some code that would randomly select a function from a
> list of functions and call it. I was looking in the globals names space and
> randomly selecting items that were of type function.. but I didn't see a
> way of actually running the function.
>
> Any pointers?
>
Unless I've missed your meaning:
>>> import random
>>> def f(): print 42
...
>>> def g(): print 24
...
>>> funcs = [f,g]
>>> random.choice(funcs)()
24
Best,
Brian vdB
More information about the Python-list
mailing list