0

this is the assignment Write a function partial that takes another function and a value as in data. Partial should return a new function with the same function as the given function but there the first argument to the given functuin is bound to a value that was given as second argument to partial

run example:
>>> def add(n, m): return n + m
...
>>> add_five = partial(add, 5)
>>> add_five(3)
8
>>> add_five(16)
21

i dont quite understand the assignment and i am new to function in function but ive done this so far and i think im on the right way?

def add(n,m):
   return n+m

def partial(func,number):
    def add_n(number):
        return func(0,number)+number
    return add_n
Jim
  • 61
  • 1
  • 8

2 Answers2

0

So first of all :

  • What does partial should do ?

It allows You to incremantaly build new function from another with some arguments already applied. Like In your case you always add something to number 5.

Good answer can be found here : Currying in Python

So answer for your question about how implemnt own partial function will be :

def add(n,m):
    return n+m

def partial(func,number):
    def add_n(arg):
        return func(number,arg)
    return add_n
Take_Care_
  • 1,957
  • 1
  • 22
  • 24
0

I would recommend reading this link. This shows you how the partial function is written so you can either write your own or use the already made one.

Using the already made one can be done with:

from functools import partial

add_n = partial(add, 5)
thialfi
  • 46
  • 4