Skip to main content
Python supports multi-paradigm programming, allowing functions to be treated with first-class citizens status. Let’s explore how to write advanced and powerful functions.
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:
  1. Assign them to new variables.
  2. Store them inside lists, tuples, or dictionaries.
  3. Pass them as arguments to other functions.
  4. Access their built-in attributes (like __name__).
Here are examples demonstrating functions behaving as objects:

Lambda Functions

A lambda function is a small, anonymous (unnamed) function defined using the lambda keyword.

Syntax

  • Lambda functions can have any number of arguments, but only one single expression.
  • The expression is evaluated and returned automatically (no return keyword 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:
Bad Practice Warning: While you can format single expressions across multiple lines using parentheses, writing complex nested conditionals or multi-line expressions inside a lambda is considered bad practice. It significantly hurts code readability and makes debugging difficult. Always prefer standard def functions for any logic that spans multiple lines or requires complex conditions.

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 the def keyword:
Lambdas are best used as quick arguments for higher-order functions like sorted(), map(), or filter():

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:
  1. You must have a nested function.
  2. The nested function must refer to a value defined in the enclosing function.
  3. 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