Tsallaka zuwa abun ciki
Justice Tech School

Python You Need: Decorators

A decorator wraps a function. Django uses them for login_required and require_POST. This pure Python version shows the idea.

Example
def shout(fn):
    def wrapper(*args):
        return fn(*args).upper()
    return wrapper

@shout
def greet(name):
    return "hello " + name

print(greet("ada"))

Result

HELLO ADA

Exercise

Apply a decorator.

shout\ndef f(): pass

Quiz Test

What does a decorator do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.