Concepts / Higher-Order Functions

Higher-Order Functions

A lambda statement creates an anonymous function object with one parameter and one expression.

  • Programming

A Function as a Value

A lambda statement creates an anonymous function object with one parameter and one expression. Because the resulting function object can be passed as an argument, a lambda can control how another function performs a computation. This is especially useful when a short function is needed only once, such as the key function supplied to sort().

createspassed as keycallsevaluateslambda x: x * 2 + 1function expressionfunction objectanonymous callablesort()receives key functionx = 3argument binds to parameter73 * 2 + 1
What happens when a lambda function object is created, passed to a higher-order function, and then called?

Tracing a Lambda Call

What do you think happens?

Suppose a lambda has the form lambda x: x * 2 + 1 and is called with 3. What result should it return?

  • 6
  • 7
  • 9
Reveal answer

Answer: 7

The argument 3 becomes the value of x. Substituting gives 3 * 2 + 1. Multiplication happens before addition, so the result is 6 + 1, which is 7.

Evaluating One Lambda Call

Trace the call of lambda x: x * 2 + 1 with the argument 3.

Bind the argument: The argument 3 becomes the value of the parameter x.

Substitute: Replace x in the expression with 3, producing 3 * 2 + 1.

Follow operation order: Evaluate multiplication before addition: 3 * 2 is 6, then add 1.

Return the expression result: The expression is the entire function body, so its result is returned immediately.

7

A reliable tracing method is always the same: start with the argument, bind it to the parameter, substitute the parameter into the expression, and evaluate the expression in its normal order of operations. A lambda has no separate sequence of statements to execute. Its single expression is the entire function and its return value.

Lambda Syntax

The basic pattern is lambda parameter : expression. The keyword lambda begins the statement, the parameter names the input, the colon separates the parameter from the expression, and the expression is evaluated and returned whenever the lambda is called.

followed byfollowed byintroduceslambdastarts the statementxinput name:separates input andexpressionx * 2 + 1body and return value
How do the parameter, colon separator, and single expression fit together to form a lambda statement?
python

The expression does not merely describe what the lambda should do later; it is the whole function body. When the lambda is called, the expression is evaluated using the argument supplied for the parameter, and the resulting value is returned.

Passing Functions to sort()

A practical use of a lambda is providing the key parameter to sort(). The key parameter tells sort() which value to use for comparison. A lambda can be written inline when the comparison rule is short and is not needed elsewhere.

items.sort(key=lambda item: item * 2)

passed assupplied toprovidesevaluateslambda item: item * 2key computationkeyfunction inputsort()uses comparison valuesitemargument valueitem * 2computed key
How does a function object move into another function and control which computation is performed?

Lambda or def

LambdaRegular function with def
AnonymousHas a name
Written inlineDefined separately
Limited to one expressionCan span multiple lines
Useful for a simple, one-time functionUseful for reusable functions and complex logic

Use a lambda when the operation is simple, short, and often passed directly as an argument. Use def when the function should have a name, be reused, contain multiple statements, or express complex logic. The choice is not about whether either form can create a callable function object; both can. The choice is about clarity, scope, and use case.

python

Mistakes in Lambda Tracing

  • Treating the colon as the return value

    The colon separates the parameter from the expression. The expression after the colon is the body and return value.

    Fix: Read the pattern as keyword, parameter, colon, then one expression.

  • Forgetting to substitute the argument

    The parameter x receives the argument value before the expression is evaluated.

    Fix: Write the substitution explicitly: 4 * 2.

  • Ignoring operation order

    Multiplication happens before addition, so these expressions do not produce the same result.

    Fix: Evaluate the substituted expression using its normal order of operations.

  • Using a lambda for complex or reusable logic

    A lambda is intended for a simple, one-time function and is limited to a single expression.

    Fix: Use a named def function when the logic is complex or needs to be reused.

Practice the Substitution Method

EASY

Trace lambda number: number * 3 + 2 when it is called with 4. Write the parameter binding, the substituted expression, and the final result.

Hints
  • First bind number to 4.
  • Then replace number in the expression.
  • Evaluate multiplication before addition.
MEDIUM

Decide whether a lambda or a named def function is clearer for a reusable operation that contains multiple statements. Explain your choice using the differences in naming, length, and complexity.

Hints
  • Consider whether the function is reusable.
  • Consider whether one expression is enough.
  • A named function can span multiple lines and contain complex logic.

Key Takeaways

  1. A lambda statement creates an anonymous function object.
  2. Its structure is lambda, one parameter, a colon, and one expression.
  3. The expression is the entire function body and is returned when the lambda is called.
  4. To trace a lambda, bind the argument to the parameter, substitute it into the expression, and evaluate the result in order.
  5. Use lambda for simple, one-time functions passed as arguments; use def for named, reusable, or complex logic.

Key Takeaways

  • A lambda creates an anonymous callable function object.
  • The parameter receives the argument, and the single expression is evaluated and returned.
  • Lambda is especially useful for short functions passed to operations such as sort().
  • A regular def function is clearer when logic is complex, spans multiple statements, or must be reused.
  • Substitution and normal operation order provide a dependable way to predict lambda results.