Fundamentals 7 min read

Master Python Functions: Definitions, Scope, Closures, and Lambdas Explained

This article explains Python functions in depth, covering their definition with def, parameter handling including default, variable-length, and keyword arguments, the role of return statements, scope rules, inner functions and closures, anonymous lambda expressions, and the built‑in filter function, illustrated with clear diagrams.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Functions: Definitions, Scope, Closures, and Lambdas Explained

1. Function Definition

Functions are organized, reusable code blocks that implement a single or related functionality.

Function blocks start with the def keyword, followed by the function name and parentheses ().

All input parameters must be placed inside the parentheses; they can be defined there.

The first line of a function may optionally contain a docstring to describe the function.

The function body starts after a colon and is indented.

A return [expression] ends the function, optionally returning a value; a bare return returns None.

When using default parameters, if a call omits a value, the default is used.

Variable-length arguments. A name prefixed with an asterisk (*) collects all unnamed positional arguments; you may also omit extra arguments.

All arguments in Python are passed by reference; modifying a parameter inside the function also changes the original argument.

The return statement exits the function, optionally returning an expression; a bare return returns None.

Inner functions. Functions can be defined inside other functions.

2. Function Variable Scope

Variables defined inside a function have local scope; variables defined outside have global scope.

Local variables are accessible only within the function where they are declared, while global variables can be accessed throughout the program. When a function is called, all variable names declared inside are added to its scope.

3. Python Closures

If an inner function references a variable from the outer (non‑global) scope, the inner function is considered a closure. A closure occurs when function A returns function B; the returned function B is the closure, and the arguments passed to A become free variables.

4. Anonymous Functions (lambda)

Python uses lambda expressions to create anonymous functions. lambda is a single expression, making the function body much simpler than def.

The body of a lambda is an expression, not a code block, and can only encapsulate limited logic.

A lambda has its own namespace and cannot access variables outside its parameter list or the global namespace.

Although a lambda appears on one line, it is not equivalent to C/C++ inline functions, which aim to reduce stack usage for performance.

Syntax: lambda [arg1 [, arg2, ... argn]]: expression Example:

5. Using the Built‑in filter Function

Official documentation: filter(function, iterable) Constructs an iterator from elements of iterable for which function returns true. iterable may be a sequence, a container supporting iteration, or an iterator. If function is None, the identity function is assumed, removing elements that are false.

The function argument can be None, a function, or a lambda expression; iterable must be an iterable object.

If function is None, it returns all elements of the iterable that are not False.

If function is a function or lambda, it returns elements for which the function returns True.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonLambdafunctionsfilterclosures
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.