Python Functions: From def to Decorators and Generators
Functions are the primary unit of code reuse in Python. Understanding them well means understanding not just def and return, but the full spectrum: how arguments work (positional, keyword, *args, **kwargs, positional-only), how closures capture state, how decorators wrap behavior, and how generators turn functions into lazy iterators.
This path moves from defining your first function through the advanced patterns that production Python code relies on. Every article includes runnable examples.
Defining and Calling Functions
6 articlesDefining Functions in Python
Function syntax, parameters, return values, docstrings, and best practices for writing clean function signatures.
Using def in Python
What def does under the hood, function objects, first-class functions, and how Python treats functions as values.
Functions vs Methods in Python
The practical and conceptual differences between standalone functions and methods bound to objects.
Modular Python Functions
Designing functions for reusability, composability, and testability -- writing code that works well in larger projects.
Create Reusable Code Patterns in Python
Design patterns for building function libraries, utility modules, and reusable abstractions.
Python Type Hints: Write Code That Explains Itself
Annotating function parameters and return types, Optional and Union, built-in generics, and the modern Python 3.12 syntax for cleaner, self-documenting signatures.
Arguments and Parameters
6 articlesPython Keyword Arguments
Named arguments, default values, and how keyword arguments improve function call readability and flexibility.
Arbitrary Argument Lists in Python
Using *args and **kwargs to create functions that accept any number of arguments.
Python Default Argument Values
Default parameters, the mutable default argument trap, and safe patterns for optional function arguments.
Python / and * Parameters
Positional-only and keyword-only parameter syntax using / and * separators in function signatures.
What Is *args and **kwargs in Python?
Complete explanation of argument unpacking, when to use each, and how they interact with other parameter types.
Python Variadic Arguments: *args, **kwargs, and Beyond
How *args and **kwargs work under the hood, keyword-only parameters, argument unpacking, CPython internals, and type annotations with ParamSpec and TypedDict.
Lambda, Closures, and Functional Patterns
7 articlesPython Lambda vs def
When lambda expressions make sense, when they don't, and the readability trade-offs of anonymous functions.
Python Lambda Loop Closure
The classic closure bug with lambdas in loops, why it happens, and how to fix it with default arguments.
Function Composition in Python
Chaining functions together, building pipelines, and implementing compose and pipe patterns.
functools and Pipe: Function Composition in Python
Using functools.reduce, partial, and third-party pipe libraries for functional composition patterns.
Python filter() Function
Filtering iterables with filter(), lambda predicates, and when to use filter vs list comprehensions.
Python map() Function
Transforming iterables with map(), combining with lambda, and comparing map to comprehension alternatives.
Functional Python Guide
Functional programming principles applied to Python: immutability, pure functions, higher-order functions, and practical patterns.
Decorators, Generators, and Advanced Patterns
8 articlesPython Decorators Demystified
How decorators work at every level -- from basic wrappers to parameterized decorators and class decorators.
Python yield
Generator functions, lazy evaluation, yield expressions, and building memory-efficient data pipelines.
Recursive Generators in Python
Combining recursion with generators using yield from for tree traversal, nested structures, and recursive pipelines.
Python send() Method: Generators as Coroutines
Using send() to push values into generators, building coroutine patterns, and two-way generator communication.
How Python's generator.send() Works
How yield became an expression in PEP 342, why generators must be primed before sending, and when to use send() over next().
Python Coroutines
Coroutine fundamentals, the evolution from generators to async/await, and how coroutines enable concurrent programming.
Understanding Lazy Evaluation in Python
How lazy evaluation works with generators, itertools, and deferred computation patterns for memory efficiency.
Python close() Method: Complete Guide
The close method on generators, files, and connections -- resource cleanup and context management patterns.