Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a random function as an argument?

Python is so flexible, that I can use functions as elements of lists or arguments of other functions. For example:

x = [sin, cos]
y = s[0](3.14) # It returns sin(3.14)

or

def func(f1, f2):
   return f1(2.0) + f2(3.0)

However, it is not clear to me how to do the same with random functions. For example I want to use Gaussian distributions: [random.normalvariate(3.0, 2.0), random.normalvariate(1.0, 4.0)]. In this example I will get a fixed list containing two elements. But what I want to get, is a list with random elements. What is a good way to do it in python?

like image 409
Roman Avatar asked Mar 11 '13 09:03

Roman


People also ask

How do you pass a function as an argument?

Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action.

Can we pass function as an argument?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.

How many arguments a random () method accepts?

random. choice() accepts one argument: the list from which you want to select an item. You may encounter a scenario where you want to choose an item randomly from a list.

How do you pass a function as a parameter in Python?

Methods are passed as arguments just like a variable. In this example, we define a class and its objects. We create an object to call the class methods. Now, to call a passed method or function, you just use the name it's bound to in the same way you would use the method's (or function's) regular name.


3 Answers

Try it with lambda functions:

[lambda: random.normalvariate(3.0, 2.0), lambda: random.normalvariate(1.0, 4.0)]

You see the difference with parentheses. sin is a function, sin(x) is the return value of this function. As you cannot create a function without parentheses representing random.normalvariate(1.0, 4.0), you have to define it as a lambda function.

like image 171
eumiro Avatar answered Oct 01 '22 14:10

eumiro


Use functools.partial or lambda

Those are basically the same:

[lambda: normalvariate(3, 2), ...]
# or
[partial(normalvariate, 3, 2), ...]

They are both equivalent to:

def _function():
    return normalvariate(3, 2)

[_function, ...]

partial is more flexible, gives you much better control over creation of _function and lets you avoid lambda syntax clutter.

By the way, there were some controversions over lambda in the Python community, but in the end Guido admitted that finding a superior alternative to the lambda expression is "an impossible quest."

like image 25
Jakub M. Avatar answered Oct 01 '22 15:10

Jakub M.


You should use partial from functools:

import functools
arg_sets = [(3.0, 2.0), (1.0, 4.0)]
myfuncs = [functools.partial(random.normalvariate, *args) for args in arg_sets]
myfuncs[0]()
like image 43
Simon Bergot Avatar answered Oct 01 '22 13:10

Simon Bergot