[SOLVED] Function restriction by fixing an argument


How should I make function with lesser dimensionality than the original one by fixing an
argument of it:

For example I want to make successor function out of sum function as follows:

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

Now I am looking for something like this:

g = f(~, 1) which would be the successor function, i.e. g(x) = x+1.


You can write your own function:

def g(y):
    return f(2, y)

Or more concisely:

g = lambda y: f(2, y)

There’s also functools.partial:

import functools

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

g = functools.partial(f, 2)

You can then call it as before:

>>> g(3)

Answered By – Blender

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *