Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a function as a variable with one input fixed

Tags:

python

Say I have a two dimensional function f(x,y) and another function G(function) that takes a function as an input. BUT, G only takes one dimensional functions as input and I'm wanting to pass f to G with the second variable as a fixed parameter.

Right now, I am just declaring a third function h that sets y to a set value. This is what it looks like in some form:

def f(x,y):
   something something something
   return z;

def G(f):
    something something something

def h(x):
   c= something
   return f(x,c);
G(h)

At some point I was also making y a default parameter that I would change each time.

Neither of these are as readable as if I was somehow able to call

G(f(x,c))

that particular syntax doesn't work. What is the best way to do this?

like image 426
albiero Avatar asked Feb 25 '14 23:02

albiero


People also ask

How do you pass a function variable to a function?

variable is in the scope of startFunction and its value is passed to adding function. variable2 is only in the scope of function adding where the value of 1 is added to the value of variable. you return the value of variable2 but there is no variable to receive that value.

Can you pass a function as a parameter in Python?

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.

Can you pass a function into a function?

Functions can be passed into other functions Functions, like any other object, can be passed as an argument to another function.

How do you pass a variable to a function?

A simple example of passing variables into a function is below. When you declare a function, you can also declare some arguments for the function by putting them inside the function () brackets. You can use whatever letter/name you like to represent the arguments - when you CALL the function, you pass through the variables.

How to pass a function as an argument to another function?

In Python, just like a normal variable, we can pass a user-defined function as an argument to another function. A function that accepts another function as its parameter is called a Higher-order function. Let’s see how we can implement this through Python code. result = foo ('Welcome To AskPython!!')

Can a function take a variable number of arguments?

However, a function can also take a variable number of arguments. Let’s look at a fixed number of arguments first. The number of arguments passed in a function call are equal to the number of parameters in the function definition. In all the previous examples, we passed a fixed number of arguments in the correct order.

How to pass a function as an object in C++?

In C++ 11, there is a std::function<> template class that allows to pass functions as objects. An object of std::function<> can be created as follows.


2 Answers

An ideal solution would use partial application, but the quickest and easiest way to accomplish this would be to wrap f inside a lambda statement like this:

G(lambda x: F(x, C))

In this example, the lambda syntax creates an anonymous function that accepts one argument, x, and calls f with that value x and the constant C. This works because the value of C is "captured" when the lambda is created and it becomes a local constant inside the lambda.

like image 88
ApproachingDarknessFish Avatar answered Nov 13 '22 03:11

ApproachingDarknessFish


The functools.partial function can be used to do this (note, it's not entirely clear where c comes from in your example code, so I've assumed it's some constant).

import functools

def f(x,y):
    return x+y

c = 3

G = functools.partial(f, c)
G(4)

I think this is more explicit than the lambda approaches suggested so far.

Edit: replacing the right most argument is not possible as we are dealing with positional arguments. Depending on the level of control available, you could introduce a wrapper which handles the switching:

import functools

def f(x,y):
    return x+y

def h(c,y):
    return f(y,c)

c = 3

G = functools.partial(h, c)
G(4)

But I think you start to sacrifice readability and maintainability at this point...

like image 31
Mark Streatfield Avatar answered Nov 13 '22 05:11

Mark Streatfield