Python: Pass a function as a variable with one input fixed
Question: 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?


Answer: The
[functools.partial](http://docs.python.org/2/library/functools.html#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...

