How to randomly choose method from a list of methods with different arguments?

python randomly select n elements from list
random.sample python
python random choice multiple
python random selection from list without repetition
random.choice python
np.random.sample python
how to randomly select a variable in python
random function in python

I have object a of class b with methods foo(), boo(), noo(arg). Now I want to randomly choose one of them without extending the class b. The problem is that if I use technics supposed here: Syntax to call random function from a list , then I have to have the same arguments of my methods. So I can't use smth like this:

my_list = [,, a.noo]    

I case of noo method I need to call it with arg: a.noo(arg). So how to do it in pythonic way?

import random

funcs = [foo, boo, noo]
args = [[1,2,3], [4,5], [2]]
choice = random.randint(len(funcs))


This way you have the flexibility to call any function with dynamic arguments, depending on your methods.

Remember to keep the arguments and the functions in the same order

Edit: You can also do as suggested in the comments (which i think it's easier, thank you!):

import random

calls = [(foo, [1,2,3]), (boo, [4,5]), (noo, [2])]
choice = random.choice(calls)


Python Random choices() Method, Method. ❮ Random Methods The choices() method returns a list with the randomly selected element from the specified sequence. You can weigh the possibility of each result with the weights parameter or the cum_weights parameter. The sequence can be a string, a range, a list, a tuple or any other kind of sequence. Example. Return a list with 14 items. The list should contain a randomly selection of the values from a specified list, and there should be 10 times higher possibility to select "apple" than the other two:

This is how I would do it. Although if you want to have functions with more than 1 argument it's probably best to send it as a list and unpack it at the function.

import random

def foo():

def boo():

def noo(arg):
    print('noo:', arg)

my_list = [(foo,), (boo,), (noo, 'hello world')]
for i in range(10):
    choice = random.choice(my_list)
    if len(choice) == 1:

Invent Your Own Computer Games with Python, 4th Edition, list(favorites.values()) ['apples', 42, 'cats'] Using list() with the keys() or values() methods, you can get a list of just The string method split() will return a list of strings with one word each. Randomly Choosing from a List The choice() function in the random module takes a list argument and returns a random value from it. With regard to the question of how to take a random sample in actual practice, we could, in simple cases like the one above, write each of the possible samples on a slip of paper, mix these slips thoroughly in a container and then draw as a lottery either blindfolded or by rotating a drum or by any other similar device.

Something like this was suggested in comment:

import random

my_list = [,, a.noo]  
rand = random.choice(my_list)
if rand.__name__ == 'noo':

Python random.sample() to choose multiple items from any sequence, sample() function has two arguments, and both are required. The population can be any sequence such as list, set from which you want to select  Arguments. The random.sample() function has two arguments, and both are required. The population can be any sequence such as list, set from which you want to select a k length number. The k is the number of random items you want to select from the sequence. k must be less than the size of the specified list.

Data Science from Scratch: First Principles with Python, More frequently we'll use the iteritems() method, which lazily yields the 2 arguments and returns an element chosen randomly from the corresponding range(): choose randomly from range(3, 6) = [3, 4, 5] There are a few more methods will probably be different) If you need to randomly pick one element from a list you  The Next() Method of System.Random class in C# is used to get a random integer number. This method can be overloaded by passing different parameters to it as follows: Next() Next(Int32) Next(Int32, Int32) Next() Method. This method is used to returns a non-negative random integer.

Programming in Prolog, Hoare's Quicksort method is described in his paper in Computer Journals Exercise 7.5: Verify that, when given a known list as LI, permutation(Ll,L2) will done at each recurrence of quisort is more than the other methods, because it The three predicates are random, which generates a pseudo-randomly chosen integer  In a school, it’s easy to get a list of all students. So the easiest option is systematic sampling. First, get a list of all the kids’ names in each class. (Make sure the list of names has been randomized!) Then you’ll pick children’s names off the list at random intervals.

Python, Function · Python - List XOR · Difference between input() and raw_input() functions in Let's discusses all different ways to select random values from a list​. This method is used to generate a random number in a range, for lists, we can specify But it differs by the fact that it required 2 mandatory arguments for range​. randomly putting sample subjects into different groups where they all have an equal chance of being in each group ex. equal chance of being in either placebo group or real group - dont really use in SS imp. diff between random sample and assigment

  • Why don't you use an if do the a.noo case ?
  • but is this pythonic way?
  • If the solution works for you, it doesn't need to be 'pythonic'
  • I mean, it might be shorter. Can't I specify somehow the arg for noo function in a list?
  • You need to call a random function, but each call should be made with a particular set of arguments, is that it?