2

I'm learning this language hence I'm new with Python. The code is:

def add(a, b):
    return a + b

def double_add(x, a, b):
    return x(x(a, b), x(a, b))

a = 4
b = 5
print(double_add(add, a, b))  

The add function is simple, it adds two numbers. The double_add function has three arguments. I understand what is happening (With some doubts). The result is 18. I can't understand how double_add uses add to function.

The question is, what is the connection between these two functions?

It would be helpful if tell me some examples of using a function as an argument of another function.
Thanks in advance.

1
  • 1
    add function is being passed as x. x(a,b) is basically add(a,b) Commented Jan 24, 2020 at 11:48

7 Answers 7

3

In python language, functions (and methods) are first class objects. First Class objects are those objects, which can be handled uniformly.

So, you just pass a method as an argument.

Your method will return add(add(4, 5), add(4, 5)) which is add(9, 9) and it's equals to 18.

Sign up to request clarification or add additional context in comments.

Comments

1

A function is an object just like any other in Python. So you can pass it as argument, assign attributes to it, and well maybe most importantely - call it. We can look at a simpler example to understand how passing a function works:

def add(a, b):
    return a + b

def sub(a, b):
    return a - b

def operate(func, a, b):
    return func(a, b)

a = 4
b = 5
print(operate(add, a, b))  
print(operate(sub, a, b))
operate(print, a, b)

And this prints out:

9
-1
4 5

That is because in each case, func is assigned with the respective function object passed as an argument, and then by doing func(a, b) it actually calls that function on the given arguments.


So what happens with your line:

return x(x(a, b), x(a, b))

is first both x(a, b) are evaluated as add(4, 5) which gives 9. And then the outer x(...) is evaluated as add(9, 9) which gives 18.

1 Comment

"That is because in each case, func is assigned with the respective function object, and then when doing func(a, b) it actually calls that function on the given arguments.", this line is what I was looking for. Thanks for the clear explanation.
1

If you would add print(x) in the double_add function you would see that it would print <function add at 0x10dd12290>. Therefore, the code of double_add is basically the same as if you would do following:

print(add(add(a,b), add(a,b))) # returns 18 in your case

Comments

1

Functions are objects in Python, just like anything else such as lists, strings.. and you can pass them same way you do with variables.

Comments

0

The function object add is passed as an argument to double_add, where it is locally referred to as x. x is then called on each, and then on the two return values from that.

Comments

0
def double_add(x, a, b):
    return x(x(a, b), x(a, b))

Let's write it differently so it's easier to explain:

def double_add(x, a, b):
    result1 = x(a, b)
    result2 = x(a, b)
    return x(result1, result2)

This means, take the function x, and apply it to the parameters a and b. x could be whatever function here.

print(double_add(add, a, b))  

Then this means: call the double_add function, giving itaddas the first parameter. Sodouble_add`, would do:

result1 = add(a, b)
result2 = add(a, b)
return add(result1, result2)

Comments

-1

This is a very simple example of what is called "dependency injection". What it means is that you are not explicitly defining an interaction between the two functions, instead you are defining that double_add should use some function, but it only knows what it is when the code is actually run. (At runtime you are injecting the depedency on a specific function, instead of hardcoding it in the function itself),

Try for example the following

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def double_add(x, a, b):
    return x(x(a, b), x(a, b))

a = 4
b = 5

print(double_add(add, a, b))  
print(double_add(subtract, a, b))  

In other words, double_add has become a generic function that will execute whatever you give it twice and print the result

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.