Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

decorators-intro

Decorators intro

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

An orientation to the Python track—how the compiled playground works, core vocabulary, and what you will practice next.

You need a clear map of the Python track so indentation, mutability, imports, and the stdlib do not feel like magic.

You will apply Decorators intro in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true). Also read the interview prep blocks.

Excellent first language—finish JavaScript basics if you already know the web, then Django for server-rendered apps.

A decorator wraps a function or class to extend behavior—syntax @decorator above def. Functions are first-class objects in Python, enabling higher-order patterns like middleware in the Django track.

Function decorator

def log_calls(fn):
    def wrapper(*args, **kwargs):
        print(f"calling {fn.__name__}")
        return fn(*args, **kwargs)
    return wrapper

@log_calls
def add(a, b):
    return a + b

functools.wraps

from functools import wraps

def deco(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        return fn(*args, **kwargs)
    return wrapper

@wraps preserves metadata (__name__, docstring) on the wrapper—important for debugging and introspection.

Important interview questions and answers

  1. Q: What does @decorator do?
    A: Rebinds the name to decorator(original_function)—syntactic sugar for wrapper assignment.
  2. Q: Decorator with arguments?
    A: Requires an outer factory returning the actual decorator—three nested functions.

Self-check

  1. Why use functools.wraps?
  2. Are decorators compile-time or runtime?

Tip: Use @wraps(fn) in decorators—preserves __name__ for pytest and debugging.

Interview prep

What @deco does?

Replaces function with deco(original)—applied at function definition time.

functools.wraps?

Copies metadata to wrapper so introspection and pytest see original __name__.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • @wraps why?
  • Decorator order?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump