Courses/Iterators, Generators & Decorators

Iterators, Generators & Decorators

Decorators

Wrap functions to add behavior.

A decorator is a function that takes another function and returns an enhanced version. Use @decorator as a shortcut.

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

@shout
def greet(name):
    return f"hi {name}"

print(greet("ada"))  # HI ADA
main.py
Output
Press Run to execute.
Expected output
14

Sign in to track your progress across lessons.