In Python, functions are first-class objects. This means that just like integers, strings, or lists, a function is an object in memory with a type, identity, and value. You can:
- Assign them to new variables.
- Store them inside lists, tuples, or dictionaries.
- Pass them as arguments to other functions.
- Access their built-in attributes (like
__name__).
Lambda Functions
A lambda function is a small, anonymous (unnamed) function defined using thelambda keyword.
Syntax
- Lambda functions can have any number of arguments, but only one single expression.
- The expression is evaluated and returned automatically (no
returnkeyword is used).
Real-World Inline Lambda Example
For quick, single-line mapping operations like adding 18% GST to prices:Limitations with Multi-line Logic
Python lambda functions are syntactically restricted to a single expression. They cannot contain statements, loops, or variable assignments. However, you can write a multi-line expression (such as nested conditional ternaries) by wrapping the lambda body inside parentheses().
1. Multi-line Lambda Expression (Nested Conditionals)
For classifying numerical scores into categories using nested ternary expressions formatted across multiple lines:2. Normal Function Alternative for Multiple Statements
If your logic requires executing multiple separate statements (like logs, assignments, or loops), you must define a normal function using thedef keyword:
Variable-Length Arguments (*args and **kwargs)
When defining functions, you can accept an arbitrary number of arguments:
*args(Positional): Collects additional positional arguments into a tuple.**kwargs(Keyword): Collects additional keyword arguments into a dictionary.
Closures
A closure is a nested function that retains access to variables from its enclosing (outer) function’s scope, even after the outer function has finished executing. To create a closure:- You must have a nested function.
- The nested function must refer to a value defined in the enclosing function.
- The enclosing function must return the nested function.
Decorators
A decorator is a design pattern in Python that allows you to modify or extend the behavior of a function or class without permanently changing its source code. Under the hood, a decorator is a higher-order function that takes another function as an argument, wraps it with additional behavior, and returns the wrapper.Writing a Custom Decorator
Decorating Functions with Arguments
To decorate functions that take arguments, use*args and **kwargs inside the wrapper function:
Practice & Exercises
To reinforce what you’ve learned in this section (Higher-order functions, lambdas, closures, and custom decorators), practice with these interactive notebooks:Follow-Along Practice
Practice passing functions as arguments, defining single-expression anonymous lambdas, sorting coordinates dynamically, building closure scopes, and applying custom decorators.💻 VS Code | 🚀 Colab | 📥 Download
Practice Exercises
Test your knowledge with hands-on exercises on higher-order operations, sorting product list prices using lambda keys, creating greeting closure builders, and writing bold-formatting decorators.💻 VS Code | 🚀 Colab | 📥 Download
What’s next?
Now let’s explore Pythonic programming patterns: comprehensions, the iterator protocol, generators, and context managers!Comprehensions
Learn comprehensions, iterators, and context managers