Pylerno
English

Decorators

Add small reusable behavior around a function without defining a class.

On this page

The concept

A closure is a function that retains access to relevant names in its enclosing scope. make_multiplier returns a function configured by a factor. A decorator accepts a function and returns a replacement callable, often a wrapper that forwards arguments and results.

@announce is equivalent to rebinding greet to announce(greet) after the definition. functools.wraps preserves identifying metadata such as the original name. A wrapper must return the original result when that is part of the contract; merely calling the function loses its return value.

Worked example

python
from functools import wraps


def make_multiplier(factor):

    def multiply(number):
        return number * factor

    return multiply


def announce(function):

    @wraps(function)
    def wrapper(*args, **kwargs):
        print("Calling the function.")
        return function(*args, **kwargs)

    return wrapper


@announce
def greet(name):
    return f"Hello {name}"


double = make_multiplier(2)
print(double(7))
print(greet("Dana"))

Trace the result

  1. make_multiplier(2) creates a configured function, then double(7) calls it.
  2. announce builds a wrapper that emits an event message and forwards arguments.
  3. The decorated greeting returns the original greeting result through the wrapper.
text
14
Calling the function.
Hello Dana
Line-by-line walkthrough

Line 1: from functools import wraps

Import wraps from functools. This supplies function wrappers and caching. The caller can now use the imported names directly.

Lines 4–9: def make_multiplier(factor):

Define the function make_multiplier with input names factor. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Lines 6–7: def multiply(number):

Define the function multiply with input names number. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 7: return number * factor

Evaluate number * factor and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 9: return multiply

Evaluate multiply and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 12–19: def announce(function):

Define the function announce with input names function. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Lines 15–17: def wrapper(*args, **kwargs):

Define the function wrapper with input names , *args, **kwargs. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 16: print("Calling the function.")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 17: return function(*args, **kwargs)

Evaluate function(*args, **kwargs) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 19: return wrapper

Evaluate wrapper and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 23–24: def greet(name):

Define the function greet with input names name. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 24: return f"Hello {name}"

Evaluate f'Hello {name}' and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 27: double = make_multiplier(2)

Evaluate make_multiplier(2), then bind or update double with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 28: print(double(7))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 29: print(greet("Dana"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Decorators can hide side effects if overused. A direct function call is often clearer when there is no repeated wrapping requirement.

Check your understanding

What does this program print?

python
def scale(k):
    return lambda n: n * k
print(scale(3)(4))
Choose one answer

Search documentation

Share this page