Skip to main content
  1. About
  2. For Teams
Asked
Modified 2 months ago
Viewed 132 times
-4

I'm learning about higher-order functions, specifically lambda functions in Python. I understand lambda functions to an extent, but a little confused about this lambda higher-order function example.

Why am I getting 6 as the output, I'm just trying to understand this mathematically

>>> high_ord_func = lambda x, func: x + func(x)
>>> high_ord_func(2, lambda x: x * x)
6

I got this problem from Real Python article. Please, I just want to make sense of this, thank you

5
  • 4
    Sorry, but you first. Please explain your code as far as you can. Then we will know exactly what you find confusing.
    quamrana
    –  quamrana
    2025-08-18 20:11:48 +00:00
    Commented Aug 18 at 20:11
  • 3
    What result are you expecting it to evaluate to?
    Carcigenicate
    –  Carcigenicate
    2025-08-18 20:14:30 +00:00
    Commented Aug 18 at 20:14
  • 1
    What do you think the func argument of high_ord_func is? If you write sq = lambda x: x * x, what do you think sq(2) will be?
    jjramsey
    –  jjramsey
    2025-08-18 20:17:50 +00:00
    Commented Aug 18 at 20:17
  • 3
    maybe write it as normal functions and you will see how it works. It allows to add print() to see what code is doing.
    furas
    –  furas
    2025-08-18 20:29:28 +00:00
    Commented Aug 18 at 20:29
  • 1
    You might find it a little clearer if you used a different name in the second statement: high_ord_func(2, lambda y: y * y) . The functionality and result are unchanged.
    user19077881
    –  user19077881
    2025-08-18 21:15:26 +00:00
    Commented Aug 18 at 21:15

3 Answers 3

5

You can consider high_ord_func as taking 2 arguments, the first is the value of x, and the second is the form of a function to evaluate.

Your second line is evaluating that function for x = 2 and func = x*x

This expands as:

x + func(x) = x + x*x = 2 + 2*2 = 6
Sign up to request clarification or add additional context in comments.

Comments

0

Below is simple implementation of what you want to achive with High order function


>>> def func(x: int) -> int:
...     return x * x
...
>>> from collections.abc import Callable
>>>
>>> def high_ord_func(x: int, func: Callable) -> int:
...     return x + func(x)
...
>>> high_ord_func(2, func)
6
>>>

so when you calling high_ord_func by passing 2 and func as argument, you expect 2 + func(2) as return. which on further evaluation of func(2) we got 4, thus making 2+4 and resulting into 6

5 Comments

Thank you guys, sorry I didn't clarify my post enough. It makes sense now
if it helped then you could choose one answer and mark it as accepted.
I accepted the answer. Anyway the moderators can remove the downvote for my original question?
@Cordev87 as far as i know NO. but dont worry, just dont stop asking questions.
Yeah I got downvoted even more, probably for suggesting this. I'll just keep my mouth shut then.
0

This is a matter of properly understanding the syntax of the lambda function.
A lambda function is defined as follows: lambda input:output

So in your code

high_ord_func = lambda x, func: x + func(x)

Let's take the part about the lambda function

lambda x, func: x + func(x)

This function takes two inputs

  • x

  • func

And it returns: x + func(x)

This implies that x is a variable and func is a callable (a function)

Then this lambda function is assigned to the function high_ord_func

This is similar to

def high_ord_func(x, func):
    return x + func(x)

Then, when you call

high_ord_func(2, lambda x: x * x)

The value of x is 2 (the first argument)

And func is the function lambda x: x * x which is a square function (it returns the square of its input).

So the output is: 2 + func(2) = 2 + 2*2 = 6

1 Comment

This last answer explains a lot better, thank you. One last thing, I'm a little rusty on my math. Why is the function a square function, that is x*x in the lambda?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.